Back to blog
Apr 16, 2025
3 min read

Astro Content Collections

Astro’s content collections provide a structured, type-safe way to organize, validate, and query Markdown content using schemas and the `config.ts` file.

📦 What Are Content Collections?

Content collections in Astro are a powerful way to organize and manage structured content—like Markdown or MDX files—in a type-safe and scalable manner. Introduced in Astro 2.0, this feature lets you group related content, define frontmatter schemas for validation, and query your content programmatically using Astro’s built-in Content APIs.


⚙️ What Does config.ts Do?

The config.ts file is where you define and configure your content collections. It enables you to:

  1. Define Collections – Group similar content types such as blog posts, product pages, or documentation.
  2. Add Schema Validation – Use the Zod library to validate frontmatter fields, ensuring consistency and preventing errors.
  3. Leverage TypeScript – Get IntelliSense, type-checking, and better DX with strongly typed content.

🧩 Example Configuration

Here’s what a basic config.ts setup might look like:

import { defineCollection, z } from 'astro:content';

const blogCollection = defineCollection({
  schema: z.object({
    title: z.string(),
    description: z.string(),
    pubDate: z.date(),
    tags: z.array(z.string()).optional(),
  }),
});

export const collections = {
  blog: blogCollection,
};

This sets up a blog collection with required fields like title, description, and pubDate. Every Markdown file in src/content/blog/ will need to match this structure.


🚀 Why Use Content Collections?

  1. Stay Organized – Group content by type and purpose.
  2. Get Type Safety – Catch schema errors early with validation.
  3. Simplify Data Access – Use getCollection() to easily filter or retrieve content.
  4. Improve the Developer Experience – Enjoy autocompletion and real-time error checking while working with frontmatter.

📁 Example Folder Structure

If your content folder looks like this:

src/
└── content/
    └── blog/
        ├── post-1.md
        └── post-2.md

The config.ts file ensures both posts follow the same validated structure, keeping things clean and consistent.


Astro’s content collections give you a structured, scalable way to manage content that grows with your project—without sacrificing flexibility or developer happiness.