📦 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:
- Define Collections – Group similar content types such as blog posts, product pages, or documentation.
- Add Schema Validation – Use the Zod library to validate frontmatter fields, ensuring consistency and preventing errors.
- 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?
- Stay Organized – Group content by type and purpose.
- Get Type Safety – Catch schema errors early with validation.
- Simplify Data Access – Use
getCollection()
to easily filter or retrieve content. - 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.