What are Content Collections?
A collection is a set of data with a similar structure, such as blog posts, product listings, or author profiles. Collections can contain:- Markdown, MDX, Markdoc files
- JSON, YAML, TOML files
- Remote data from a CMS or API
TypeScript Configuration
Content collections require specific TypeScript settings. Ensure yourtsconfig.json includes:
Defining Collections
Create asrc/content.config.ts file to define your collections:
Built-in Loaders
glob() Loader
Loads files from directories using glob patterns:file() Loader
Loads multiple entries from a single file:parser option for custom file types:
Custom Loaders
Create inline loaders for remote data:Schema Definition
Schemas enforce data validation using Zod:Collection References
Reference entries from other collections:Querying Collections
Use helper functions to query your collections:Filtering Queries
Filter entries based on criteria:Sorting Collections
Sort entries manually as needed:Rendering Content
Render Markdown and MDX content using therender() function:
Generating Routes
Static Sites
UsegetStaticPaths() to generate pages:
SSR Sites
Fetch entries on demand:Type Safety
UseCollectionEntry type for components:
Accessing Referenced Data
Query referenced entries separately:JSON Schemas
Astro generates JSON Schema files for collections in.astro/collections/. Use them in your editor for IntelliSense:
When to Use Collections
Use content collections when you:- Have multiple files sharing the same structure
- Need type safety and validation
- Want optimized querying for thousands of entries
- Need to fetch remote content from a CMS
- Have only one or a few unique pages
- Are displaying unprocessed static files
- Need real-time data updates