Astro Content Collections 实战指南
为何使用 Content Collections
通过 defineCollection 与 zod,我们可以在开发阶段就捕获 frontmatter 错误。
关键优势
- 类型安全与自动补全
- 构建期校验,提前发现问题
- 内容与页面解耦,复用性更强
import { defineCollection, z } from 'astro:content';
export const collections = {
blog: defineCollection({
type: 'content',
schema: z.object({ title: z.string(), pubDate: z.date() }),
}),
}; # astro# content# zod