· updated

Setting Up Content Collections in Astro

How type-safe content collections power the blog on this site.

astrotypescript

Content collections give you type-safe frontmatter and a single source of truth for structured content. The blog you’re reading is powered by one.

Defining the collection

Each collection declares a loader and a Zod schema:

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

Querying posts

Pages pull entries with getCollection and render MD/MDX with render:

const posts = await getCollection("blog");

That’s the whole pipeline — fully typed, from frontmatter to rendered page.