Skip to content

@genga-cms/react-router

Genga for React Router in framework mode. One resource route serves the Genga paths (the admin app, its API, the media, MCP), and loadPage reads the document of a page in its loader.

Package React Router Node
@genga-cms/react-router ^7.18.4 or ^8.5.2 >=22.12

The version of @genga-cms/react-router is the version of genga.

Set up

app/routes.ts
route('/admin/*', 'routes/genga.ts', { id: 'genga-admin' }),
route('/media/*', 'routes/genga.ts', { id: 'genga-media' }),
// app/routes/genga.ts
import { gengaResourceRoute } from '@genga-cms/react-router';
import { genga } from '../.server/genga';
const route = gengaResourceRoute(genga);
export const loader = route.loader;
export const action = route.action;
// app/routes/post.tsx
import { Blocks, usePreviewData } from '@genga-cms/react';
import { loadPage } from '@genga-cms/react-router';
export const loader = ({ request, params }: Route.LoaderArgs) => loadPage(genga, request, 'posts', params.slug);
export const headers = ({ loaderHeaders }: Route.HeadersArgs) => loaderHeaders;
export default function Post({ loaderData }: Route.ComponentProps) {
const { data } = usePreviewData(loaderData.doc, loaderData.preview);
return <Blocks blocks={data.body} registry={blocks} preview={loaderData.preview} />;
}

WARNING: do not write export const { loader, action } = gengaResourceRoute(genga). React Router cannot remove a destructured loader from the browser build, and the build fails.

  • gengaResourceRoute(genga, { unavailable }) takes the instance, or a function that gives it. If the function gives null (the CMS is off), the route answers unavailable(request), by default a 503.
  • loadPage returns the document in the public shape (absolute media URLs, rich text with html) with its ETag and Cache-Control. It throws a 301 to the new path of a renamed slug, and a 404. With ?genga-preview=<id>, it gives the draft to a signed-in editor of the same origin. With a remote source (createRemoteSource of genga/remote), it checks the preview token.
  • The routes need no admin route module, no genga/admin.css and no data-genga-admin in the root layout. To move an app of genga 0.3, read the migration guide.