Data, content, and assets
Create a content collection
Group related content entries under one typed model rather than reading arbitrary files throughout page templates.
8 minute lesson
Content collections give repeated content one loading and validation boundary. They fit posts, documentation, products, and any group whose entries share a shape.
In Astro 7, define build-time collections in src/content.config.ts. A loader says where the data comes from:
import { defineCollection } from "astro:content"
import { glob } from "astro/loaders"
const notes = defineCollection({
loader: glob({
base: "./src/content/notes",
pattern: "**/*.{md,mdx}"
})
})
export const collections = { notes }
Each Markdown file under that directory becomes an entry with an id, its frontmatter in data, and renderable content. The collection name notes becomes the stable handle used by pages.
The loader is required. glob() loads separate local files, file() can turn one JSON, YAML, or TOML file into entries, and custom loaders can retrieve remote content.
This is better than scattering filesystem reads across page templates. Routes can ask for a typed collection without knowing its storage details. If the source later moves from Markdown to a CMS, that change stays at the loading boundary rather than spreading across every component.
Lesson completed