Working with Astro Content Collections
Astro’s content collections provide a type-safe way to manage markdown content.
Defining a Collection
Collections are defined in src/content/config.ts with Zod schemas:
import { defineCollection, z } from 'astro:content';
const blog = defineCollection({
type: 'content',
schema: z.object({
title: z.string(),
pubDate: z.date(),
category: z.enum(['tech', 'daily']),
}),
});
Querying Content
---
const posts = await getCollection('blog');
const sorted = posts.sort(
(a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf()
);
---
This gives you full type inference on frontmatter fields — autocomplete and type checking included.