Mastering Content Collections in Astro
Managing content in Astro using the @astro/content module is a game-changer for structured sites.
π Overview
Astro provides a powerful and type-safe way to manage your content with:
- Collections
- Schemas
- Markdown/MDX parsing
- Static typing (Zod)
π οΈ Getting Started
To set up content collections:
// src/content/config.ts
import { defineCollection, z } from "astro:content";
export const collections = {
blog: defineCollection({
type: "content",
schema: z.object({
title: z.string(),
slug: z.string(),
description: z.string(),
tags: z.array(z.string()),
pubDate: z.date(),
}),
}),
};
β Features
-
Schema Validation Ensures consistent metadata across posts.
-
Type Inference IntelliSense in your IDE.
-
Custom Fields You can extend the schema to support authors, reading time, or cover images.
πΈ Embedding Images
You can use standard markdown syntax:
π Tables
| Feature | Status |
|---|---|
| MDX Support | β |
| Schema Typed | β |
| RSS Integration | β |
π§ Bonus Tips
Always define a unique
slugto prevent route conflicts.
π Deploying Your Content
Once everything is set up:
npm run build && npm run preview
Youβll now have statically generated pages based on your Markdown.