Content collections
A content collection is a set of records of the same kind: blog posts, authors, projects. You describe where the files are and which fields their frontmatter must have, and Astro checks every file and gives you handy functions to query them. In Astro 5 collections are configured in one file, src/content.config.ts.
Post files
Posts are ordinary Markdown files. Create the folder src/content/blog/ and the file src/content/blog/first-post.md in it:
---
title: First post
description: How this blog started.
pubDate: 2025-03-10
tags: [astro, blog]
---
Hi! This is the first post of my blog.Add one or two more posts in the same format, for example learning-astro.md. Files in src/content/ do not become pages by themselves.
Defining the collection
Create the file src/content.config.ts (in the src folder, next to pages):
// src/content.config.ts
import { defineCollection, z } from 'astro:content';
import { glob } from 'astro/loaders';
const blog = defineCollection({
loader: glob({ pattern: '**/*.md', base: './src/content/blog' }),
schema: z.object({
title: z.string(),
description: z.string(),
pubDate: z.coerce.date(),
tags: z.array(z.string()).default([]),
draft: z.boolean().default(false),
}),
});
export const collections = { blog };Restart npm run dev. Astro reads all .md files from src/content/blog and checks their frontmatter. If everything is fine, nothing special appears in the terminal.
What the definition consists of
| Part | What it does |
|---|---|
loader | The loader: where entries come from. glob() finds files by a path pattern |
schema | The schema: which fields are required and of what type |
export const collections | Declares the collections; the key blog is the collection name |
The schema is written with the zod library, whose z object Astro re-exports. z.coerce.date() turns the string 2025-03-10 into a Date object, and .default([]) puts in an empty array when a file has no tags.
Besides glob() there is the file() loader: it takes all entries from a single JSON or YAML file, such as a list of authors.
What the schema gives you
If a post is missing the title field or has a date written as yesterday, Astro stops with a clear error naming the file and the field. Also, the editor learns the field types and suggests them when you query.
Common mistake
People put the config file in the project root or use the old name src/content/config.ts. In Astro 5 it is called src/content.config.ts and sits directly in src/. If Astro says a collection was not found, check the file name and location, and the name in export const collections.