Skip to content

@genga-cms/next

Genga for the Next.js App Router, in two modes:

  • Embedded: Genga runs inside the Next.js server, with the admin under /admin of the site. One long-running Node process and one Postgres database.
  • Remote: the site reads a Genga server over its public API. Use it on serverless hosts, on more than one instance of the site, or for a static site.

Published pages stay static and change at each publish. Editors see the live preview of the real page in the Genga admin.

Package Next.js React Node Runtime of the routes
@genga-cms/next >=16.4 <17 ^19.0 >=22.12 Node.js

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

Set up: embedded mode

// next.config.ts: genga and postgres stay in node_modules
import { withGenga } from '@genga-cms/next/config';
export default withGenga({});
// lib/genga.ts: one instance per process, created at the first request
import 'server-only';
import { createEmbeddedSource, gengaInstance } from '@genga-cms/next';
import { createGenga } from 'genga/server';
export const embeddedGenga = gengaInstance(() =>
createGenga(config, { databaseUrl: process.env.DATABASE_URL!, siteUrl: 'https://www.example.com' }),
);
export const genga = createEmbeddedSource(embeddedGenga);
// app/admin/[[...path]]/route.ts and app/media/[...key]/route.ts (and app/mcp/route.ts with the mcp option)
export const { GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS } = gengaRoute(embeddedGenga);
// instrumentation.ts: the migrations run at start
export async function register() {
if (process.env.NEXT_RUNTIME === 'nodejs') await (await import('./lib/genga')).embeddedGenga().ready();
}

The preview route, the layout and the pages are the same as in remote mode, without the webhook route. Set previewUrl of each collection to https://www.example.com/api/genga/preview?path={path}.

Set up: remote mode

lib/genga.ts
import 'server-only';
import { createRemoteSource } from '@genga-cms/next';
import config from '@/genga.config.mjs';
export const genga = createRemoteSource({ url: process.env.GENGA_URL!, schema: config });
// app/api/genga/preview/route.ts and app/api/genga/webhook/route.ts
export const { GET, POST, DELETE } = previewRoute(genga);
export const { POST } = webhookRoute(); // reads GENGA_WEBHOOK_SECRET
// app/layout.tsx, in <body>: connects the preview pages to the editor
<GengaPreviewBridge source={genga} />;
// app/blog/[slug]/page.tsx
export const generateStaticParams = async () => [];
export default async function Post({ params }: { params: Promise<{ slug: string }> }) {
const page = await loadPage(genga, 'posts', (await params).slug);
if (page.kind === 'redirect') permanentRedirect(page.location);
if (page.kind === 'not-found') notFound();
return <Blocks blocks={page.doc.data.body} registry={blocks} preview={page.preview} />;
}

previewRoute, webhookRoute, GengaPreviewBridge, loadPage and Blocks come from @genga-cms/next; notFound and permanentRedirect from next/navigation. On the Genga server, set previewUrl of each collection to https://www.example.com/api/genga/preview?path={path}, GENGA_PREVIEW_ORIGINS to the site origin, and GENGA_WEBHOOKS to [{"url":"https://www.example.com/api/genga/webhook","secret":"whsec_…"}].

How it works in embedded mode

  • Reads. createEmbeddedSource reads in process, and keeps published reads in the Data Cache of Next.js (unstable_cache) with the Genga cache tags, for one hour at most (maxAge).
  • Publish. gengaRoute and createEmbeddedSource add one listener per instance to the publish events. A publish runs in a request of the admin, and the listener calls revalidateTag(tag, { expire: 0 }) for each tag of the event. A scheduled publish runs outside a request: its pages change after maxAge.
  • Preview. The admin opens the entry route on the same origin, without a token. The route checks the admin session, turns Draft Mode on, and keeps only the document id in the genga-preview cookie. loadPage checks the session again at each request.
  • Admin. The routes serve the prebuilt admin app and its own CSP. If proxy.ts sets a CSP, its matcher must exclude /admin and /media.
  • Hosting. One long-running Node process (next start, a container). Serverless hosts and more than one instance need remote mode.

How it works in remote mode

  • Reads. loadPage reads Draft Mode first, and the cookies only in Draft Mode, so public pages stay static. Published reads go to the Data Cache with the Genga cache tags, for one hour at most (maxAge).
  • Publish. The webhook route checks the Standard Webhooks signature, then calls revalidateTag(tag, { expire: 0 }) for each tag of the event.
  • Preview. The editor opens the entry route with a preview token. The route checks the token with the Genga server, turns Draft Mode on, and keeps the token in an HttpOnly cookie. In production both cookies are SameSite=None; Secure; Partitioned, so the preview works in the admin frame also when the admin is on another site (Chrome, Firefox, Safari). Browsers that block every third-party cookie (GNOME Web) need the admin on the same site, for example cms.example.com for www.example.com. The token never goes in the page URL.
  • Blocks. defineBlockComponents(config, { hero: Hero }) maps block types to components. In a preview, a Client Component block ('use client') follows each keystroke. A Server Component block shows the draft after each autosave, when <GengaPreview> refreshes the page. usePreviewData from @genga-cms/next/client gives the live draft to your own Client Components.
  • Rich text. RichText renders the HTML of the allowlist renderer of Genga.

Before production

  • CDN. Published pages send the ISR header of Next.js (s-maxage). revalidateTag clears the cache of Next.js only: purge your CDN on the webhook (onEvent), or do not cache the pages there. More than one instance of the site needs a shared cacheHandler.
  • Rate limit. All reads of the site come from one client address. Raise GENGA_RATE_LIMIT on the Genga server for the site, or exempt it.
  • Frame. If the site sends a CSP, allow the admin origin in frame-ancestors for the preview. Turn analytics off when page.preview is true, and add robots: { index: false }.
  • A missed webhook expires after maxAge: the adapter does not read /api/v1/events yet.

See examples/next for a complete site, and the adapter specification.