@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
/adminof 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_modulesimport { withGenga } from '@genga-cms/next/config';export default withGenga({});
// lib/genga.ts: one instance per process, created at the first requestimport '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 startexport 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
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.tsexport 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.tsxexport 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.
createEmbeddedSourcereads 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.
gengaRouteandcreateEmbeddedSourceadd one listener per instance to the publish events. A publish runs in a request of the admin, and the listener callsrevalidateTag(tag, { expire: 0 })for each tag of the event. A scheduled publish runs outside a request: its pages change aftermaxAge. - 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-previewcookie.loadPagechecks the session again at each request. - Admin. The routes serve the prebuilt admin app and its own CSP. If
proxy.tssets a CSP, itsmatchermust exclude/adminand/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.
loadPagereads 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
HttpOnlycookie. In production both cookies areSameSite=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 examplecms.example.comforwww.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.usePreviewDatafrom@genga-cms/next/clientgives the live draft to your own Client Components. - Rich text.
RichTextrenders the HTML of the allowlist renderer of Genga.
Before production
- CDN. Published pages send the ISR header of Next.js (
s-maxage).revalidateTagclears 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 sharedcacheHandler. - Rate limit. All reads of the site come from one client address. Raise
GENGA_RATE_LIMITon the Genga server for the site, or exempt it. - Frame. If the site sends a CSP, allow the admin origin in
frame-ancestorsfor the preview. Turn analytics off whenpage.previewis true, and addrobots: { index: false }. - A missed webhook expires after
maxAge: the adapter does not read/api/v1/eventsyet.
See examples/next for a
complete site, and the adapter specification.