How does Astro handle content collections?

Answer

Content Collections in Astro provide a type-safe API for managing Markdown, MDX, and other structured content. Create a collection by putting files in src/content/blog/. Define a schema in src/content/config.ts: import { z, defineCollection } from 'astro:content'; const blog = defineCollection({ type: 'content', schema: z.object({ title: z.string(), date: z.date(), draft: z.boolean().optional() }) }); export const collections = { blog };. Query in a page: import { getCollection } from 'astro:content'; const posts = await getCollection('blog', ({ data }) => !data.draft);. Render a post: const { Content } = await post.render();. Benefits: Type safety: frontmatter is validated against the Zod schema at build time. TypeScript types: autocomplete for collection entries. Filtering and sorting: query with predicates. Content Collections replace ad-hoc glob imports and eliminate runtime frontmatter parsing errors.