#astro
#content
Content Collections in Astro
1 min read
Astro’s Content Layer is a great way to manage blog posts with type safety.
Defining a collection
In src/content.config.ts, define a collection with a glob loader:
import { defineCollection, z } from "astro:content";
import { glob } from "astro/loaders";
const blog = defineCollection({
loader: glob({ pattern: "**/*.{md,mdx}", base: "./src/content/blog" }),
schema: z.object({
title: z.string(),
pubDate: z.coerce.date(),
tags: z.array(z.string()).default([]),
}),
});
export const collections = { blog };
Querying posts
const posts = await getCollection("blog");
Every post is fully validated and typed. Give it a try!