Define Collection in Astro

From the Astro cheat sheet · Content Collections · verified Jul 2026

Define Collection

typescript
// src/content.config.ts
import { defineCollection, z } from 'astro:content';
import { glob } from 'astro/loaders';

const blog = defineCollection({
  loader: glob({ pattern: "**/*.md", base: "./src/data/blog" }),
  schema: z.object({
    title: z.string(),
    pubDate: z.date(),
    author: z.string(),
    tags: z.array(z.string()),
    draft: z.boolean().optional(),
  }),
});

export const collections = { blog };
✅ defineCollection() configures collection with loader and schema
💡 Use glob() loader for file-based content
🔍 Zod schema validates frontmatter at build time
⚡ Store content in any directory, specify with base option
contentcollectionsschemadefineCollection

More Astro tasks

Back to the full Astro cheat sheet