-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Content Collections #5291
Content Collections #5291
Conversation
🦋 Changeset detectedLatest commit: 36ffb39 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
e118e1a
to
2defe68
Compare
a801044
to
94c6792
Compare
|
day: 'numeric', | ||
})} | ||
</time> | ||
<a href={'/blog/' + post.slug}>{post.data.title}</a> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems there's no longer a post.url
to use now? Does it mean we have to build it ourselves?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When using getCollection
, post.slug
is the replacement for post.url
! .url
will stick around if you ever import Markdown or MDX in src/pages/
using ESM, but we've added this new solution for anything in src/content/
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After using these APIs more, we might revisit the type signature for imported Markdown and MDX. We already have some funky translations:
frontmatter
->data
url
->slug
rawContent()
->body
I bet we could align these!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My point is Astro is able to calculate the full url here, so it may not make much sense for users to build the full url themselves. For example, say I have trailingSlash: 'always'
enabled in astro.config.mjs
, now I would need to take care of the trailing slash now:
<a href={'/blog/' + post.slug + '/'}>
If I change my mind to rename blog
to any other names, I have to update those /blog/
as well.
While in before, I can just use:
<a href={post.url}>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chenxsan Actually, we can't calculate the full URL here. Let me explain!
Today, the url
property is generated for all Markdown and MDX in your src/pages/
directory. Since we know exactly what route will be generated by using src/pages/
, we can return the full URL. That also means url is undefined for Markdown and MDX outside of src/pages/
, like a src/posts/
directory. This is frustrating for devs that prefer splitting their content out separately, and wire them up to routes using a [...dynamic]
page.
Now let's talk about slug
. These are generated based on entries in src/content/
(not src/pages/
) based on file path relative to the collection directory. So src/content/blog/post.md
will have a slug of post
.
This is a big improvement over an undefined url
if you were storing in a src/posts/
or some equivalent before. It also gives you the freedom to place your content under any route you choose using getStaticPaths
. For instance, say we want our src/content/blog
entries to live on /news/blog/*
. We can create a /news/blog/[...slug].astro
like so:
export function getStaticPaths() {
const posts = await getCollection('blog');
return posts.map(post => { params: { slug: post.slug }, props: post });
}
...and generate /news/blog/first-post
, /news/blog/second-post
, etc. Main takeaway: you can organize your src/content/
collections however you'd like, without the collection name influencing the URL.
Of course, this has the compromise of losing that base /news/blog/
in the generated slug. It's up to you to construct this based on how your getStaticPaths
works.
All APIs have tradeoffs! We felt this was the best compromise to fix the undefined url
problem mentioned earlier, while giving you the flexibility to map content entries to any URL for your choosing.
If this is insufficient, I'm also working on a way to override our default slug
s to generate your own at the collection level. Stay tuned for that!
1834bca
to
25cf020
Compare
This comment was marked as spam.
This comment was marked as spam.
31159b8
to
ddd4f7f
Compare
!preview content-schemas |
|
!preview content-schemas |
|
91f7b80
to
345727e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR is blocked because it contains a minor
changeset. A reviewer will merge this at the next release if approved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR is blocked because it contains a minor
changeset. A reviewer will merge this at the next release if approved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR is blocked because it contains a minor
changeset. A reviewer will merge this at the next release if approved.
318fad9
to
a4ee6a3
Compare
cb4da1a
to
6dae520
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR is blocked because it contains a minor
changeset. A reviewer will merge this at the next release if approved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR is blocked because it contains a minor
changeset. A reviewer will merge this at the next release if approved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR is blocked because it contains a minor
changeset. A reviewer will merge this at the next release if approved.
Changes
src/content/
, with the helper functionsrenderContent
andfetchContent
to efficiently retrieve entrieswith-content
example to showcase these APIsSee RFC for full details and usage instructions!
Testing
getStaticPaths
integration testsrender()
style bleed testsDocs
Guide: withastro/docs#2141
API reference: TODO