Explore Astro 5 collections with the glob() loader, custom loaders, Zod schemas, the content store, and getCollection/getEntry
0 / 5 completed
1 / 5
What does the Content Layer API introduced in Astro 5 generalise?
Content Layer: in Astro 5 decouples collections from src/content. A collection is defined with a loader, which can be the built-in glob() / file() loader for local content or a custom loader fetching from a remote API or CMS. Data is cached in a content store, improving build performance for large sites.
2 / 5
What is the role of the glob() loader in a content collection?
glob() loader:defineCollection({ loader: glob({ pattern: '**/*.md', base: './src/posts' }) }) loads every matching file as an entry. The base sets the directory, and the entry id derives from the path. Unlike legacy collections, this works from any folder, decoupling content location from the framework convention.
3 / 5
How are content collection schemas defined in Astro?
Collection schemas:defineCollection({ loader, schema: z.object({ title: z.string(), pubDate: z.date() }) }). Astro validates each entry's frontmatter against the Zod schema during the build and fails fast on mismatches. The schema also drives the inferred types returned by getCollection() and getEntry(), giving end-to-end type safety.
4 / 5
What does a custom loader object provide to the Content Layer?
Custom loaders: return { name, load }. The load({ store, logger, parseData, generateDigest }) function fetches data, validates each item with parseData, and calls store.set() to persist entries. The store is incrementally cached between builds, so a loader can use a digest to skip unchanged entries and speed up rebuilds.
5 / 5
How do you query Content Layer collections in a page?
Querying:const posts = await getCollection('blog') returns typed entries with data matching the schema. const post = await getEntry('blog', slug) fetches one. For rendered body content, call const { Content } = await render(post) (Astro 5) and render <Content />. These APIs work identically regardless of the underlying loader source.