Optional: Make a content collection
本頁內容尚未翻譯。
Now that you have a blog using Astro’s built-in file-based routing, you will update it to use a content collection. Content collections are a powerful way to manage groups of similar content, such as blog posts.
Get ready to…
- Move your folder of blog posts into
src/blog/
- Create a schema to define your blog post frontmatter
- Use
getCollection()
to get blog post content and metadata
Learn: Pages vs Collections
Section titled Learn: Pages vs CollectionsEven when using content collections, you will still use the src/pages/
folder for individual pages, such as your About Me page. But, moving your blog posts outside of this special folder will allow you to use more powerful and performant APIs to generate your blog post index and display your individual blog posts.
At the same time, you’ll receive better guidance and autocompletion in your code editor because you will have a schema to define a common structure for each post that Astro will help you enforce through Zod, a schema declaration and validation library for TypeScript. In your schema, you can specify when frontmatter properties are required, such as a description or an author, and which data type each property must be, such as a string or an array. This leads to catching many mistakes sooner, with descriptive error messages telling you exactly what the problem is.
Read more about Astro’s content collections in our guide, or get started with the instructions below to convert a basic blog from src/pages/posts/
to src/blog/
.
Test your knowledge
Section titled Test your knowledge-
Which type of page would you probably keep in
src/pages/
? -
Which is not a benefit of moving blog posts to a content collection?
-
Content collections uses TypeScript …
The steps below show you how to extend the final product of the Build a Blog tutorial by creating a content collection for the blog posts.
Upgrade dependencies
Section titled Upgrade dependenciesUpgrade to the latest version of Astro, and upgrade all integrations to their latest versions by running the following commands in your terminal:
Create a collection for your posts
Section titled Create a collection for your posts-
Create a new collection (folder) called
src/blog/
. -
Move all your existing blog posts (
.md
files) fromsrc/pages/posts/
into this new collection. -
Create a
src/content.config.ts
file to define a schema for yourpostsCollection
. For the existing blog tutorial code, add the following contents to the file to define all the frontmatter properties used in its blog posts: -
In order for Astro to recognize your schema, quit (
CTRL + C
) and restart the dev server to continue with the tutorial. This will define theastro:content
module.
Generate pages from a collection
Section titled Generate pages from a collection-
Create a page file called
src/pages/posts/[...slug].astro
. Your Markdown and MDX files no longer automatically become pages using Astro’s file-based routing when they are inside a collection, so you must create a page responsible for generating each individual blog post. -
Add the following code to query your collection to make each blog post’s slug and page content available to each page it will generate:
-
Render your post
<Content />
within the layout for Markdown pages. This allows you to specify a common layout for all of your posts. -
Remove the
layout
definition in each individual post’s frontmatter. Your content is now wrapped in a layout when rendered, and this property is no longer needed.
Replace import.meta.glob()
with getCollection()
Section titled Replace import.meta.glob() with getCollection()-
Anywhere you have a list of blog posts, like the tutorial’s Blog page (
src/pages/blog.astro/
), you will need to replaceimport.meta.glob()
withgetCollection()
as the way to fetch content and metadata from your Markdown files. -
You will also need to update references to the data returned for each
post
. You will now find your frontmatter values on thedata
property of each object. Also, when using collections eachpost
object will have a pageslug
, not a full URL. -
The tutorial blog project also dynamically generates a page for each tag using
src/pages/tags/[tag].astro
and displays a list of tags atsrc/pages/tags/index.astro
.Apply the same changes as above to these two files:
- fetch data about all your blog posts using
getCollection("blog")
instead of usingimport.meta.glob()
- access all frontmatter values using
data
instead offrontmatter
- create a page URL by adding the post’s
slug
to the/posts/
path
The page that generates individual tag pages now becomes:
Try it yourself - Update the query in the Tag Index page
Section titled Try it yourself - Update the query in the Tag Index pageImport and use
getCollection
to fetch the tags used in the blog posts onsrc/pages/tags/index.astro
, following the same steps as above.Show me the code.
- fetch data about all your blog posts using
Update any frontmatter values to match your schema
Section titled Update any frontmatter values to match your schemaIf necessary, update any frontmatter values throughout your project, such as in your layout, that do not match your collections schema.
In the blog tutorial example, pubDate
was a string. Now, according to the schema that defines types for the post frontmatter, pubDate
will be a Date
object. You can now take advantage of this to use the methods available for any Date
object to format the date.
To render the date in the blog post layout, convert it to a string using toLocaleDateString()
method:
Update the RSS function
Section titled Update the RSS functionThe tutorial blog project includes an RSS feed. This function must also use getCollection()
to return information from your blog posts. You will then generate the RSS items using the data
object returned.
For the full example of the blog tutorial using content collections, see the Content Collections branch of the tutorial repo.
Tutorials