Data & content · Lesson 11 of 22 · 15 min

Blog posts as a content collection

Store posts in Markdown, define a schema, and let Astro validate every post.

You could make blog posts the same way as the About page: one .md file per post in src/pages/. That works until you want a list of posts, sorted by date, with drafts hidden. For that, Astro has content collections.

A collection is a folder of Markdown files plus a schema that says what frontmatter each file must have. You get:

  • Validation. A post missing a title fails the build with a clear error.
  • Types. Your editor knows post.data.pubDate is a date.
  • A query API. getCollection('blog') returns every post.

1. Write three posts

Create src/content/blog/ and add these files. The file name becomes the post’s ID and URL.

src/content/blog/first-supper.md

---
title: Our first supper
description: How one dinner for twelve turned into a business.
pubDate: 2026-09-14
tags: [story]
---

It started with a borrowed table and a pot of beans.

## The menu

1. Charred corn with lime
2. Slow-cooked pinto beans
3. Peach cobbler

Everyone asked for the recipes. So we started writing them down.

src/content/blog/fall-menu.md

---
title: The fall menu is here
description: Squash, apples, and the return of braised greens.
pubDate: 2026-10-20
tags: [menu, seasonal]
---

Fall is the best season to cook. Here's what's on the table.

- **Roasted delicata squash** with brown butter
- **Braised greens** with smoked turkey
- **Apple hand pies**

Book a dinner before the holidays fill up.

src/content/blog/meal-prep-tips.md

---
title: Three meal prep habits that stick
description: Small moves that make weeknights easier.
pubDate: 2026-11-10
tags: [tips]
---

You don't need a whole Sunday. You need three habits.

1. **Cook one grain, one protein, one sauce.** Mix and match all week.
2. **Label everything** with the day you made it.
3. **Prep the hard part only.** Chop the onions, skip the rest.

2. Define the collection

Create src/content.config.ts. Note the location: directly in src/, not inside content/.

src/content.config.ts

import { defineCollection } from 'astro:content';
import { glob } from 'astro/loaders';
import { z } from 'astro/zod';

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 };

Reading the schema

  • loader: glob(...) tells Astro where the files are: every .md file in src/content/blog.
  • schema uses Zod, a small validation library that ships with Astro.
  • z.string() means required text.
  • z.coerce.date() turns 2026-09-14 into a real JavaScript Date.
  • .default([]) and .default(false) make a field optional with a fallback. None of your posts set draft, so they’re all false.

Restart the dev server (Ctrl + C, then npm run dev) so Astro picks up the new config.

3. Watch validation work

Delete the title: line from fall-menu.md and save. The dev server shows an error naming the file and the missing field. Put the line back.

That error would have stopped a broken post from going live.