This document describes Astro's Content Collections API and the underlying Data Layer. The system provides a unified, type-safe architecture for loading, processing, and querying content from diverse sources. It supports arbitrary data sources including local files (Markdown, MDX, JSON, YAML, TOML), remote APIs, databases, and CMS platforms through a loader-based architecture.
The Data Layer orchestrates a sync process that loads content, maintains an in-memory and on-disk data store, and generates TypeScript types to enable end-to-end type safety in content usage.
Sources: packages/astro/src/content/index.ts1-14 packages/astro/src/content/utils.ts56-121 packages/astro/src/content/consts.ts1-26
The Data Layer is organized around a "push" model where Content Loaders retrieve data and push entries into a centralized DataStore. The main coordinating class is ContentLayer, which manages the loading lifecycle, caching, and syncing to disk.
This diagram links domain concepts to specific implementations:
content.config.ts defines collection configurations using defineCollection.ContentLayer manages content syncing using loaders packages/astro/src/content/content-layer.ts46-75MutableDataStore holds data in-memory and persists to disk packages/astro/src/content/mutable-data-store.ts18-40astroContentImportPlugin transforms content files into JS modules packages/astro/src/content/index.ts12astroContentVirtualModPlugin provides the virtual module interface (astro:content) packages/astro/src/content/vite-plugin-content-virtual-mod.ts24-26createContentTypesGenerator() produces type definitions used at dev/build time packages/astro/src/content/types-generator.ts79-100Sources: packages/astro/src/content/content-layer.ts46-75 packages/astro/src/content/mutable-data-store.ts18-40 packages/astro/src/content/types-generator.ts79-100 packages/astro/src/content/vite-plugin-content-virtual-mod.ts24-191
The ContentLayer class orchestrates syncing content during astro sync, astro dev, or astro build.
PQueue to avoid concurrent data mutation packages/astro/src/content/content-layer.ts73syncInternal function handles clearing the cache if the --force flag is used packages/astro/src/core/sync/index.ts134-136Sources: packages/astro/src/content/content-layer.ts185-260 packages/astro/src/content/mutable-data-store.ts41-46 packages/astro/src/core/sync/index.ts123-184
Loaders abstract the logic for loading data from different sources into the store. They conform to a common Loader interface accepting a LoaderContext.
Loaders receive a context providing:
store: a scoped API to set entries for a collection (store.set(key, entry)) packages/astro/src/content/content-layer.ts134parseData: function to validate raw data against Zod schemas packages/astro/src/content/content-layer.ts138generateDigest: function to hash source content for change detection, using xxhash-wasm packages/astro/src/content/content-layer.ts140watcher: Vite FSWatcher instance for file watching and incremental reloads packages/astro/src/content/content-layer.ts141Sources: packages/astro/src/content/loaders/types.ts1-20 packages/astro/src/content/loaders/glob.ts77-129 packages/astro/src/content/loaders/file.ts25-129 packages/astro/src/content/runtime.ts55-92
The Data Store is the single source of truth for content during dev and build.
Map<string, Map<string, DataEntry>> to organize data by collection and entry ID packages/astro/src/content/data-store.ts30MutableDataStore extends ImmutableDataStore with mutation APIs (set, delete, clear) and atomic writes packages/astro/src/content/mutable-data-store.ts18-40.astro/data-store.json (or a chunked directory if experimental) with serialized content ensuring fast reload packages/astro/src/content/mutable-data-store.ts41-55 packages/astro/src/core/sync/index.ts99-114astro:content is resolved via the astroContentVirtualModPlugin. This module re-exports content entries for runtime consumption packages/astro/src/content/vite-plugin-content-virtual-mod.ts87-191invalidateDataStore signals the SSR runner to clear its route cache so that getStaticPaths() is re-evaluated with updated content packages/astro/src/content/vite-plugin-content-virtual-mod.ts92-94full-reload for the client environment to reflect changes packages/astro/src/content/vite-plugin-content-virtual-mod.ts99-104Sources: packages/astro/src/content/mutable-data-store.ts18-67 packages/astro/src/content/vite-plugin-content-virtual-mod.ts31-104 packages/astro/src/core/sync/index.ts99-114
TypeScript types are automatically generated to provide intellisense for collections.
createContentTypesGenerator() Workflowcontent.config.ts changes, it reloads the config observer packages/astro/src/content/types-generator.ts139-148CollectionEntryMap containing all recognized collections and their entry IDs/slugs packages/astro/src/content/types-generator.ts165-220content-types.d.ts inside the .astro directory packages/astro/src/content/types-generator.ts99-100Sources: packages/astro/src/content/types-generator.ts79-220 packages/astro/src/core/sync/index.ts202-203
Querying content at runtime is handled by functions exported from astro:content.
getCollection(collection, filter?): Retrieves all entries. If a live collection is requested via this method, it throws an error directing the user to getLiveCollection() packages/astro/src/content/runtime.ts99-108getEntry(collection, idOrSlug): Retrieves a single entry. It handles image reference updates via updateImageReferencesInData packages/astro/src/content/runtime.ts161-213updateImageReferencesInData: Recursively walks the data object to replace serialized image strings with optimized asset metadata packages/astro/src/content/runtime.ts273-306Sources: packages/astro/src/content/runtime.ts94-306
| Code Entity | Responsibility | File Path |
|---|---|---|
ContentLayer | Orchestrates loaders and sync process | packages/astro/src/content/content-layer.ts |
MutableDataStore | In-memory and on-disk data management | packages/astro/src/content/mutable-data-store.ts |
syncInternal | Core logic for the astro sync command | packages/astro/src/core/sync/index.ts |
createContentTypesGenerator | Generates .astro/types.d.ts | packages/astro/src/content/types-generator.ts |
astroContentVirtualModPlugin | Provides astro:content virtual module | packages/astro/src/content/vite-plugin-content-virtual-mod.ts |
Sources: packages/astro/src/content/content-layer.ts46-75 packages/astro/src/content/mutable-data-store.ts18-40 packages/astro/src/core/sync/index.ts123-184 packages/astro/src/content/types-generator.ts79-100 packages/astro/src/content/vite-plugin-content-virtual-mod.ts107-116
Refresh this wiki
This wiki was recently refreshed. Please wait 6 days to refresh again.