Skip to content

@genga-cms/nuxt

Genga for Nuxt. The site reads a Genga server over its public API (remote mode), or runs Genga in the Nuxt server (embedded mode). Editors see the real page in the preview of the Genga admin, and Vue blocks follow each keystroke.

Package Nuxt Vue Node Server
@genga-cms/nuxt ^4.7 ^3.5 >=22.12 Nitro 2, h3 1

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

Set up

nuxt.config.ts
export default defineNuxtConfig({
modules: ['@genga-cms/nuxt'],
genga: { source: '~~/server/genga', collections: ['pages', 'posts'] },
});
// server/genga.ts: remote mode (or `createGenga()` of genga/server for embedded mode)
import { createRemoteSource } from '@genga-cms/nuxt/remote';
import config from '../genga.config.mjs';
export default createRemoteSource({ url: process.env.GENGA_URL!, schema: config });
app/pages/blog/[slug].vue
<script setup lang="ts">
const route = useRoute();
const { doc, preview } = await useGengaPage<PostData>('posts', String(route.params.slug));
</script>
<template>
<article v-if="doc">
<h1>{{ doc.data.title }}</h1>
<GengaBlocks :blocks="doc.data.body" :registry="blocks" :preview="preview" />
</article>
</template>

blocks is defineBlockComponents(config, { hero: Hero }). 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_…"}]. The site reads the same secret from NUXT_GENGA_WEBHOOK_SECRET, on every Nitro preset. On Node, GENGA_WEBHOOK_SECRET works too.

Options

Option Default Purpose
source ~~/server/genga The server module whose default export is the source
collections [] The collections that useGengaPage can read
pageRoute /api/_genga/page The server route of useGengaPage
webhookRoute /api/genga/webhook The receiver of the publish webhooks, or false
handlePaths [] Embedded mode: ['/admin', '/media'], served by genga.handle
maxAge 3600 Seconds before a cached remote read expires without a webhook

How it works

  • Reads. useGengaPage calls the page route with useFetch, on the server and on client navigation. It forwards the Genga headers to the page, navigates to the new path with 301 for a renamed slug, and throws a 404. In a server route, useGengaSource() gives the source.
  • Cache. In remote mode, published reads stay in memory with the Genga cache tags. The webhook route checks the Standard Webhooks signature, purges the tags of the event, then calls the Nitro hook genga:event (purge a CDN there). Embedded reads use the cache of the core.
  • Preview. The page route reads the draft with the preview token (remote mode) or the admin session (embedded mode), with private, no-store. In the browser, the token goes from the address bar to memory, and each change of the editor updates doc at once.
  • Blocks. <GengaBlocks> renders each block in an error boundary (onErrorCaptured). A block component gets _id and _type only when it declares them as props. <GengaRichText> renders the HTML of the allowlist renderer of Genga.

Limits of this release

  • The read cache is in the memory of one Nitro process: each instance needs its own webhook.
  • No rebuild hook for nuxt generate (use genga:event), and no reconciliation with /api/v1/events: a missed webhook expires after maxAge.
  • The error page of Nuxt sends its own Cache-Control on a preview that fails.

See examples/nuxt and the adapter specification.