Astro strapi loader

From VirtusLab-Open-Source/astro-strapi-loader · View interactive version

Astro strapi loader is an AI agent skill from the GitHub repository VirtusLab-Open-Source/astro-strapi-loader. Use @sensinum/astro-strapi-loader with Astro Content Layer: build Strapi REST query objects (object and object-array populate per REST parameters), let qs.stringify serialize—never inline query strings. Composable query fragments, dynamic zone "on", locale strategies, generateCollections, getColle

How to use this skill

Skill file

Astro Strapi Loader — agent skill

Scope

Install and base wiring

  1. Install @sensinum/astro-strapi-loader (with peer zod / astro per your project).
  2. In src/content.config.ts, await generateCollections(config, definitions) and spread the returned record into collections (or merge multiple calls—see below).
  3. Env: STRAPI_URL, STRAPI_TOKEN when the API is protected.
  4. Token: Must allow Content API reads and Content-Type Builder GET content 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:

  1. Shared fragments — small const objects (e.g. heroPopulate, seoPopulate) and compose the final query with spread so hero/SEO stay consistent across page types.
  2. One dynamic zone map — a single const shaped as { [dzFieldName]: { on: { 'component.api.id': { populate: ... } } } }, then spread into populate (e.g. populate: { hero, seo, ...dynamicZoneFragment }). Add or trim components in one place.
  3. Dot-path populate arrays — for flat but wide graphs, populate: ['a', 'a.b', 'a.b.c'] next to nested objects in the same populate where needed.
  4. Central query module — move exported const query objects to e.g. src/globals/queries.ts (or lib/strapi-queries.ts) and import them into content.config.ts. The same object can be passed to the loader and, for runtime fetches, stringified with qs—one source of truth.
  5. 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 looping generateCollections.

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

Anti-patterns

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

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

The same StrapiCollection shape applies; routing is defined by the content type in Strapi.

Multilingual / `locale`

Using data in Astro

Native Markdown (`render()` / `<Content />`)

Opt-in via markdown on the collection / strapiLoader options (omit = unchanged behavior):

Not for Strapi Blocks JSON — only string Markdown/HTML fields.

Troubleshooting

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.