Skip to main content
Content Collections are Astro’s solution for managing content like blog posts, documentation, or any structured data. They provide type-safety, validation, and automatic TypeScript types for your content.

What are Content Collections?

Content Collections organize your content into typed, validated groups. Instead of manually loading markdown files, you define collections with schemas and let Astro handle the rest.
Key Benefits: Type-safe frontmatter, automatic TypeScript types, content validation, and optimized image handling.

Setup

Create a content.config.ts file in your src/ directory:
src/content.config.ts
Organize your content:

Defining Collections

Use defineCollection() to create a collection:

Schema Validation

Schemas use Zod for runtime validation:

Image Schemas

Use the image() helper for optimized images:
In your markdown:
src/content/blog/post.md

Querying Collections

Get All Entries

Use getCollection() to fetch all entries:
src/pages/blog/index.astro

Filter Entries

Filter with a callback function:

Get Single Entry

Use getEntry() to fetch a specific entry:
src/pages/blog/[id].astro

Rendering Content

Call render() on an entry to get the content component:
The Content component is the rendered markdown/MDX. headings contains the document outline.

Dynamic Routes

Generate routes from collections:
src/pages/blog/[...slug].astro

Loaders

Loaders determine where content comes from. The glob() loader reads files from disk:
You can also create custom loaders to fetch content from APIs, databases, or CMSs.

References Between Collections

Create relationships between collections:
src/content.config.ts
Query referenced entries:

Type Safety

Astro generates TypeScript types automatically:

Implementation Details

From the source code at src/content/runtime.ts, collections use:
  1. Data Store: Content entries are stored in a global data store
  2. Schema Validation: Zod schemas validate entries at build time
  3. Type Generation: TypeScript types are auto-generated from schemas

Practical Examples

Blog with Categories

src/content.config.ts
src/pages/blog/category/[category].astro

Documentation with Sidebar

src/content.config.ts
src/components/DocsSidebar.astro

Best Practices

1

Define clear schemas

Use descriptive field names and provide defaults where appropriate.
2

Validate early

Let Zod catch errors at build time, not runtime.
3

Use TypeScript

Take advantage of auto-generated types for type safety.
4

Organize by collection

Group similar content together (blog, docs, authors).
5

Filter in queries

Filter collections at query time for flexibility.

Use enums

Define allowed values with z.enum() for better validation.

Reference related content

Use reference() to create relationships between collections.

Optimize images

Use the image() helper for automatic image optimization.

Default values

Provide sensible defaults to make frontmatter easier to write.

Learn More

Routing

Generate routes from collections

Layouts

Create layouts for your content