Astro lets you manage similar kinds of content with content collections. They can house many different types of content, but most people only use them to house blog posts.
Here are some other examples:
- Partials
- Testimonials
- Authors (if your site contains guest posts)
- And so on…
To use content collections, you need to first define your collections.
Defining your collections
Create a content.config.js in the src folder and import defineCollection and glob. Those are the bare minimum you’ll need.
import { defineCollection } from 'astro:content'import { glob } from 'astro/loaders'Then you define your collections using both of these functions.
Here, base refers to the directory and pattern refers to the glob pattern within the directory.
// Blog collectionconst blog = defineCollection({ loader: glob({ base: './src/content/blog', pattern: '**/*.{md,mdx}', }),})
// Data collection, querying json filesconst data = defineCollection({ loader: glob({ base: './src/content/data', pattern: '**/*.json', }),})The last step is to export your collection:
export const collections = { blog, data }Then you’ll be able to use it.
Glob and Custom loaders
I only load md and mdx files most of the time. I don’t even load json because JS files are much easier to use.
I don’t use custom loaders either, because defining them is not worth the effort most of the time! That said, others have built custom loaders so check it out and see if any of them help you.
Determining schema with Zod
You can use Zod to define the schema of a content collection. Personally, I find that a hassle and skip this step altogether.
Using Content Collections
There are two main ways to use data stored in content collections:
- Get the entire collection
- Get the entry directly
Getting the entire collection
You can get all entries in the collection with getCollection.
import { getCollection } from 'astro:content'const posts = await getCollection('blog')// Do something with postsgetCollection is usually used in posts, tags, and pagination pages. We will talk about it in more detail as we explore those patterns later.
Getting the entry
An entry is one item in the collection.
To get the entry, you retrieve the entry using the id of the entry.
---import { getEntry, render } from 'astro:content'const { id } = Astro.propsconst entry = await getEntry('collection-name', id)---Normally, id is the filename (without the extension). But if you have a nested entry, id is the entire pathway, including the filename.
# Flat collectionsrc/content/blog/└── post-1.md # id is post-1
# Nested collectionsrc/content/testimonials/└── small/ └── aaron.md # id is small/aaronThere are two things that are useful in the entry:
frontmattercontent (if you use it withmdormdx)- The contents of the file
The front matter can be retrieved in the data property. For the content you need to use the render function.
---import { getEntry, render } from 'astro:content'const { id } = Astro.propsconst entry = await getEntry('collection-name', id)const { data } = entry // frontmatterconst { Content } = await render(entry) // content---We don’t need so much boilerplate code to do this, so I built a getEntry function to make things slightly easier.
---import { getEntry } from '@splendidlabz/astro/content'const { id } = Astro.propsconst { data, Content } = await getEntry('collection-name', id)---Code snippets to copy paste
Defining a content collection
// Importsimport { defineCollection } from 'astro:content'import { glob } from 'astro/loaders'
// Define collectionsconst blog = defineCollection({ loader: glob({ base: './src/content/blog', pattern: '**/*.{md,mdx}', }),})
const data = defineCollection({ loader: glob({ base: './src/content/data', pattern: '**/*.json', }),})
// Export your collectionsexport const collections = { blog, data }Getting the collection
---import { getCollection } from 'astro:content'const posts = await getCollection('blog')// Do something with posts---Getting an entry
---import { getEntry } from '@splendidlabz/astro/content'const { id } = Astro.propsconst { data, Content } = await getEntry('collection-name', id)---That’s it for content collections! Now, we’re going to use it to make a blog.
Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson