Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Practical Astro
Lesson Plan
  1. Content Systems
    1. Introduction
    2. An import alias
    3. Using Markdown
    4. Markdown extended
    5. Markdown component
    6. Markdown component in frameworks
    7. Content collections made easy
    8. Basic blogs
    9. Blogs made easy
    10. Prev/Next navigation
    11. Pagination made easy
    12. Better tag pages
    13. Paginated tag pages
    14. Markdown layouts
    15. Partials
    16. JS over JSON Data
    17. Dynamic 404 pages
    18. Putting everything together
    19. Other useful systems
  2. Image Systems
    1. Introduction
    2. Images are complicated because of this
    3. Astro's Image component
    4. Astro's Picture component
    5. Improvements over Astro's Image and Picture Components
    6. Images in Markdown and MDX
    7. Images in framework components
    8. Background images
    9. Art Direction with Astro
    10. Videos without layout shifts
    11. GIF Replacements and background video
    12. SVGs and icons
    13. SVG backgrounds
    14. Adding noise to images
    15. Asset caching on GitHub Actions
    16. Putting everything together
    17. Other useful systems

Content collections made easy

3m:37s

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 collection
const blog = defineCollection({
loader: glob({
base: './src/content/blog',
pattern: '**/*.{md,mdx}',
}),
})
// Data collection, querying json files
const 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:

  1. Get the entire collection
  2. 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 posts

getCollection 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.props
const 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.

Terminal window
# Flat collection
src/content/blog/
└── post-1.md # id is post-1
# Nested collection
src/content/testimonials/
└── small/
└── aaron.md # id is small/aaron

There are two things that are useful in the entry:

  1. frontmatter content (if you use it with md or mdx)
  2. 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.props
const entry = await getEntry('collection-name', id)
const { data } = entry // frontmatter
const { 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.props
const { data, Content } = await getEntry('collection-name', id)
---

Code snippets to copy paste

Defining a content collection

// Imports
import { defineCollection } from 'astro:content'
import { glob } from 'astro/loaders'
// Define collections
const blog = defineCollection({
loader: glob({
base: './src/content/blog',
pattern: '**/*.{md,mdx}',
}),
})
const data = defineCollection({
loader: glob({
base: './src/content/data',
pattern: '**/*.json',
}),
})
// Export your collections
export 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.props
const { data, Content } = await getEntry('collection-name', id)
---

That’s it for content collections! Now, we’re going to use it to make a blog.