Skip to main content

Getting the most out of Astro Content Collections

Content Collections at a glance

Astro’s Content Collections let you define schemas for your Markdown and MDX files using Zod. Once the schema is defined, Astro generates TypeScript types automatically — no more guessing what’s in your frontmatter.

const journal = defineCollection({
  loader: glob({ base: "./src/content/journal", pattern: "**/*.{md,mdx}" }),
  schema: z.object({
    title: z.string(),
    publishDate: z.date(),
    // ...
  }),
});

Why this matters

The schema acts as a contract between your content files and your components. If a frontmatter field is missing or in the wrong format, the build fails — not the browser at runtime.

This also means migrating to a headless CMS is straightforward: swap getCollection() for a CMS SDK call that returns the same shape.

← Back to Journal