-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Content Collections] Add
slug
frontmatter field (#5941)
* feat: respect `slug` frontmatter prop * chore: replace `slug` check with proper types * fix: regen types on `slug` change * chore: add TODO on slug gen * tests: update to use `slug` frontmatter prop * chore: add error message on `slug` inside object schema * lint * chore: add note on frontmatter parse * refactor: move content errors to new heading * chore: ContentSchemaContainsSlugError * chore: changeset * docs: be 10% less gentle Co-authored-by: Sarah Rainsberger <[email protected]> * fix: avoid parsing slug on unlink * docs: clarify old API is for beta users Co-authored-by: Sarah Rainsberger <[email protected]>
- Loading branch information
1 parent
f62ec16
commit 3048238
Showing
12 changed files
with
188 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
'astro': major | ||
--- | ||
|
||
Content collections: Introduce a new `slug` frontmatter field for overriding the generated slug. This replaces the previous `slug()` collection config option from Astro 1.X and the 2.0 beta. | ||
|
||
When present in a Markdown or MDX file, this will override the generated slug for that entry. | ||
|
||
```diff | ||
# src/content/blog/post-1.md | ||
--- | ||
title: Post 1 | ||
+ slug: post-1-custom-slug | ||
--- | ||
``` | ||
|
||
Astro will respect this slug in the generated `slug` type and when using the `getEntryBySlug()` utility: | ||
|
||
```astro | ||
--- | ||
import { getEntryBySlug } from 'astro:content'; | ||
// Retrieve `src/content/blog/post-1.md` by slug with type safety | ||
const post = await getEntryBySlug('blog', 'post-1-custom-slug'); | ||
--- | ||
``` | ||
|
||
#### Migration | ||
|
||
If you relied on the `slug()` config option, you will need to move all custom slugs to `slug` frontmatter properties in each collection entry. | ||
|
||
Additionally, Astro no longer allows `slug` as a collection schema property. This ensures Astro can manage the `slug` property for type generation and performance. Remove this property from your schema and any relevant `slug()` configuration: | ||
|
||
```diff | ||
const blog = defineCollection({ | ||
schema: z.object({ | ||
- slug: z.string().optional(), | ||
}), | ||
- slug({ defaultSlug, data }) { | ||
- return data.slug ?? defaultSlug; | ||
- }, | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...tions/src/content/with-slug-config/one.md → ...ions/src/content/with-custom-slugs/one.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
prefix: fancy | ||
slug: fancy-one | ||
--- | ||
|
||
# It's the first page, fancy! |
2 changes: 1 addition & 1 deletion
2
...ons/src/content/with-slug-config/three.md → ...ns/src/content/with-custom-slugs/three.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
prefix: excellent | ||
slug: excellent-three | ||
--- | ||
|
||
# It's the third page, excellent! |
Oops, something went wrong.