Skip to content

@genga-cms/astro

Genga for Astro. The site reads a Genga server over its public API (remote mode), or runs Genga in the Astro server (embedded mode). Published pages stay in the route cache of Astro until a publish purges them. Editors see the real page in the preview of the Genga admin.

Package Astro Node Rendering of Genga pages
@genga-cms/astro ^7.4 >=22.23.4 on demand (output: 'server')

The version of @genga-cms/astro is the version of genga.

Set up (remote mode)

src/lib/genga.ts
---
// astro.config.mjs: output 'server', an adapter, and cache: { provider: memoryCache() }
// export const genga = createRemoteSource({ url: process.env.GENGA_URL!, schema: config });
// src/pages/api/genga/webhook.ts
// export const prerender = false; export const POST = webhookEndpoint(); // GENGA_WEBHOOK_SECRET
// src/pages/blog/[slug].astro
import { loadPage } from '@genga-cms/astro';
import { Blocks, GengaPreview } from '@genga-cms/astro/components';
const page = await loadPage(Astro, genga, 'posts', Astro.params.slug);
if (page.kind === 'redirect') return Astro.redirect(page.location, 301);
if (page.kind === 'not-found') return new Response(null, { status: 404, headers: Astro.response.headers });
---
<main data-genga-root><Blocks blocks={page.doc.data.body} registry={blocks} preview={page.preview} /></main>
{page.preview && <GengaPreview documentId={page.doc.id} adminOrigin={genga.adminOrigin} />}

On the Genga server, set previewUrl of each collection to https://www.example.com{path}, GENGA_PREVIEW_ORIGINS to the site origin, and GENGA_WEBHOOKS to [{"url":"https://www.example.com/api/genga/webhook","secret":"whsec_…"}].

Embedded mode

Create Genga in the site with createGenga of genga/server, and mount it on src/pages/admin/[...path].ts and src/pages/media/[...path].ts: export const prerender = false; export const ALL = gengaEndpoint(genga);. Add vite: { ssr: { external: ['genga'] } } to the Astro config: Genga loads its admin app, postgres and sharp from node_modules. The preview uses the session of the admin, on the same origin.

How it works

  • Reads. loadPage reads the published document, a redirect or not found, and the draft for a preview URL of the editor. It copies the Genga headers (ETag, Cache-Control) to the response. cacheTags(Astro, genga.tagsFor('posts')) caches a list page.
  • Cache. Published pages go to the route cache with the Genga cache tags, for one hour at most (maxAge). The webhook endpoint checks the Standard Webhooks signature, then calls cache.invalidate({ tags }). In embedded mode, gengaEndpoint purges the tags after a publish.
  • Preview. A preview page is never cached (private, no-store). <GengaPreview> moves the token from the address bar to memory. After each autosave, and after each fresh token, it renders the page again on the server and replaces the element with data-genga-root.
  • Blocks. defineBlockComponents(config, { hero: Hero }) maps block types to .astro components. RichText renders the HTML of the allowlist renderer of Genga.

Limits of this release

  • .astro blocks show the draft after each autosave, not at each keystroke. A framework island in the registry does not hydrate: Astro needs a static import for client:*.
  • A block that throws stops the page: Astro has no error boundary for a component.
  • The webhook purges the route cache of the instance that receives it. With more than one instance, use a shared cache provider, or one webhook target per instance.
  • No live content loader, no rebuild hook for a static site (use onEvent of the webhook), and no reconciliation with /api/v1/events: a missed webhook expires after maxAge.

See examples/astro and the adapter specification.