Skip to content

Move to the prebuilt admin

The release after Genga 0.3 ships the admin as a prebuilt single-page app: genga.handle serves it under adminPath. The admin route modules of 0.3 still work, so you can move when you want. This guide is for a React Router app that mounts those route modules.

What still works

Nothing breaks when you update genga:

  • The route components of genga/admin and the genga.admin.* loaders and actions still work. At the first call, each Genga instance logs one warning: genga.admin is deprecated and leaves at 1.0.
  • genga.api, genga.media.serve and genga.mcp, which the route modules of 0.3 call for the admin API, the media and MCP, still work and log the same kind of warning.
  • If your app calls genga.handle for every request (in a middleware), set adminApp: false in createGenga until you move: genga.handle then leaves the admin pages to your route modules.
  • genga/admin.css is still in the package, for the route modules.
  • The same-origin preview with the admin session does not change.
  • genga.published.loadForRoute still works and logs the same kind of warning. loadPage of @genga-cms/react-router replaces it (step 6).

Two tests run the old route modules next to the new handler, on one database: a test of the package in React Router (packages/genga/test/legacy-routes.test.ts), and pnpm test:pack, which builds an app with the route modules of 0.3, @genga-cms/react-router and loadPage with React Router 7 and 8 (scripts/fixtures/legacy-admin).

Steps

  1. Install the adapters. npm i @genga-cms/react-router @genga-cms/react. They have the version of genga.

  2. Replace the admin routes with one route. Remove the admin route modules: shell.tsx, login.tsx, setup.tsx, app.tsx, dashboard.tsx, collection.tsx, document.tsx, media.tsx, users.tsx, tokens.tsx, oauth-authorize.tsx and api.ts, and the routes of the media and MCP (routes/media.ts, routes/mcp.ts). One module replaces them all:

    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;

    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.

    With a custom adminPath, for example /cms, use route('/cms/*', …). With the mcp option, route /mcp and /.well-known/* to the same module.

  3. Remove the admin code of the root layout. The admin page is no longer a page of your app: remove data-genga-admin, className="dark" and data-theme from <html> for the admin paths, and the link to genga/admin.css.

  4. Check your CSP. genga.handle sets a Content Security Policy on the admin pages. If your app adds its own CSP to every response, exclude the admin path from it. The admin pages need no inline script and no inline style.

  5. Remove the theme overrides of genga/admin.css. The admin app ships its own stylesheet. An override of the --genga-* variables in your app no longer reaches the admin.

  6. Load the pages with loadPage. It replaces published.loadForRoute and the documentData helper of the example: it returns the document with its cache headers, or throws a 301 for a renamed slug, or a 404. The same-origin preview still uses the session: no new secret.

    import { loadPage } from '@genga-cms/react-router';
    export const loader = ({ request, params }: Route.LoaderArgs) =>
    loadPage<PostData>(genga, request, 'posts', params.slug);
    export const headers = ({ loaderHeaders, errorHeaders }: Route.HeadersArgs) => errorHeaders ?? loaderHeaders;

    loadPage gives the public shape: media URLs are absolute (they start with siteUrl), and each rich text value also has its html. A component that compares a media URL with a path must change.

  7. Move the block components out of the config. defineBlock({ component }) still works with RenderBlocks of genga/client. With @genga-cms/react, the config holds the fields and defineBlockComponents(gengaConfig, { hero: Hero }) gives the components to <Blocks>. usePreviewData and RichText of @genga-cms/react have the same options as those of genga/client.

  8. Remove react-router imports for Genga. genga/server no longer imports react-router. If your app used the data() errors of genga.admin.* in its own code, read the plain Response that Genga now throws: a 302 with Location, or a JSON body { message } with a status.

If your code calls genga.admin.requireUser(request) to know the signed-in user, use genga.getUser(request): it gives the user or null, and logs no warning.

The OAuth consent page (<adminPath>/oauth/authorize) and the setup and invitation links are pages of the admin app: no route of their own.

What changes for the people who use the admin

Nothing: the pages, the URLs and the session cookie (__Host-genga, SameSite=Lax, Path=/) stay the same. A person who is signed in stays signed in after the update.

At 1.0

genga/admin, genga/admin.css, genga.admin.* and the react-router peer dependency of genga leave. react-router is already an optional peer: only genga/admin needs it.