首页作品博客笔记关于
← 返回

Astro Content Collections 笔记

什么是 Content Collections

Astro Content Collections 是一种类型安全的内容管理方式,通过 Zod schema 定义 frontmatter 结构,在构建时进行校验。

定义 Schema

const blog = defineCollection({
  schema: z.object({
    title: z.string(),
    pubDate: z.coerce.date(),
    tags: z.array(z.string()).default([]),
  }),
});

查询内容

const posts = await getCollection('blog');

优势

  • 类型安全,IDE 自动补全
  • 构建时校验,防止错误数据
  • 与 Astro 深度集成,性能优异