Astro Strapi Loader — agent skill
Scope
- Package:
@sensinum/astro-strapi-loader(peerastro^6 || ^7, Strapi 5). - Goal: Load Strapi at build time into Astro content collections with correct
populategraphs, dynamic zones, and i18n.
Install and base wiring
- Install
@sensinum/astro-strapi-loader(with peerzod/astroper your project). - In
src/content.config.ts,await generateCollections(config, definitions)and spread the returned record intocollections(or merge multiple calls—see below). - Env:
STRAPI_URL,STRAPI_TOKENwhen the API is protected. - Token: Must allow Content API reads and Content-Type Builder
GETcontent types (schema source for generated Zod).
Configuration object (`generateCollections` first argument)
| Field | Notes |
|--------|--------|
| url | Strapi base URL; no /api suffix (package requests /api/...). |
| token | Optional Bearer. |
| strict | Optional; stricter Zod from schema builder. |
| headers | Optional extra request headers. |
Defining entries (second argument)
Each item is one Strapi collection → one Astro defineCollection, keyed by collectionName or the Strapi type name.
| Field | Purpose |
|--------|-----------|
| name | API id / route segment (e.g. homepage, case-studies). |
| query | REST object: populate, filters, sort, fields, pagination. |
| collectionName | Astro collection key when reusing the same name (e.g. per-locale splits). |
| locale | string or string[] — see Multilingual. |
| idGenerator | (data) => string — else documentId. |
Shorthand: ['homepage', 'layout'] means default query for those types.
Merging several generateCollections results: the function returns a flat object of collection configs. You can await it once with a long definition list, or call it in a loop (e.g. one definition at a time) and spread into a single object: strapiCollections = { ...strapiCollections, ...result }. The loop pattern helps isolate failures (log per def, continue) when some endpoints error at build time.
Organizing large `query` values
Techniques that keep content.config.ts maintainable:
- Shared fragments — small
constobjects (e.g.heroPopulate,seoPopulate) and compose the finalquerywith spread so hero/SEO stay consistent across page types. - One dynamic zone map — a single
constshaped as{ [dzFieldName]: { on: { 'component.api.id': { populate: ... } } } }, then spread intopopulate(e.g.populate: { hero, seo, ...dynamicZoneFragment }). Add or trim components in one place. - Dot-path
populatearrays — for flat but wide graphs,populate: ['a', 'a.b', 'a.b.c']next to nested objects in the samepopulatewhere needed. - Central query module — move exported
constquery objects to e.g.src/globals/queries.ts(orlib/strapi-queries.ts) and import them intocontent.config.ts. The same object can be passed to the loader and, for runtime fetches, stringified withqs—one source of truth. - Single types + collection types — list single-type and collection-type defs separately, then build a combined
collectionDefs(e.g. each entry{ name, query, collectionName?, locale? }) before loopinggenerateCollections.
Building `query` objects (Strapi REST)
Author data, not URLs. Aligned with REST API parameters: filters, locale, status, populate, fields, sort, pagination. Bracket encoding in the query string is produced by qs from that object.
Object form (deep `populate`)
Nested objects for relations, components, and dynamic zones, e.g. populate: { seo: { populate: ['ogImage'] } }.
Object–array (mixed) `populate`
Parameters allow populate as string or object. Array of dot-paths is useful for flatter trees:
populate: ['logo', 'seo', 'seo.ogImage', 'customer.logo_light']
You may mix a path array and nested on / objects under one populate.
Other parameters
sort: string or string array.fields: array of names.filters: object — Filters.pagination:{ page, pageSize }or as per current Strapi docs.
Anti-patterns
- Hand-built
?a=b&...strings, template literals, or manual bracket segments. - Using
URLSearchParamsfor deep nestedpopulate. - Pre-stringifying inside
content.config.ts(the loader should receive a plain object).
Manual HTTP
import qs from "qs";
const queryParams = qs.stringify(
{ populate: { /* ... */ }, locale: "en" },
{ encodeValuesOnly: true }
);
Do not use JSON.stringify on the whole query in place of qs for the URL.
Serialization boundary
- Loader: pass one
queryobject;strapiLoaderusesqs.stringifyinternally. - Raw
fetch/fetchContent: build the same object, thenqs.stringify(..., { encodeValuesOnly: true }).
Dynamic zone with `on`
The dynamic zone field (e.g. sections, blocks) is populated with on: only listed component API ids (as in the Content-Type Builder) are expanded, each with its own populate.
{
blocks: {
on: {
"blocks.rich_text": { populate: { body: true, cta: true } },
"blocks.media_row": { populate: { items: { populate: ["media"] } } },
},
},
}
Place that under the top-level populate next to hero, seo, etc. Deep trees inside on (nested components, media) use the same rules as any other populate object.
Collection types vs single types
- Collection:
datais an array — one content entry per item in the store. - Single type:
datais a single object — one entry in the store.
The same StrapiCollection shape applies; routing is defined by the content type in Strapi.
Multilingual / `locale`
locale: 'en'(single): one request; Strapi returns that language.- Same Strapi type → multiple Astro collections: repeat the entry with the same
name, differentlocaleand differentcollectionName(e.g.pages-pl,pages-en) sogetCollection('pages-pl')andgetCollection('pages-en')are separate. locale: ['en', 'de', 'pl']: the loader fetches per locale, stores ids aslocale:documentId, adds_localeon each item’s data. Pairs well withidGeneratorwhen public ids are slugs:en:about-us.- The loader merges
localeinto the request query; you still modellocalein the object, not as a raw query string.
Using data in Astro
getCollection('key')— all entries; filter ondata._localefor multi-locale single collection.getEntry('key', 'locale:documentId' | 'locale:slug')when using prefixed ids.- Runtime Strapi calls:
fetchContentwithqs.stringify’d params, ideally reusing the same query object as build time.
Native Markdown (`render()` / `<Content />`)
Opt-in via markdown on the collection / strapiLoader options (omit = unchanged behavior):
field: dot-path to a markdown stringgetMarkdown: custom extractor (wins overfield)format: "html"for already-rendered HTML; default"markdown"uses AstrorenderMarkdown()- Helpers:
getValueByPath,resolveMarkdownSource,createHtmlRenderedContent,buildEntryRenderedContent
Not for Strapi Blocks JSON — only string Markdown/HTML fields.
Troubleshooting
- Empty or partial data:
populatedepth, wronglocale, draft/unpublished, orfilterstoo strict. - 400 on REST: compare object shape to parameters and actual serialization via
qs. - Build schema errors:
STRAPI_URL/ token cannot reach Content-Type Builder or list content types.