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/adminand thegenga.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.serveandgenga.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.handlefor every request (in a middleware), setadminApp: falseincreateGengauntil you move:genga.handlethen leaves the admin pages to your route modules. genga/admin.cssis still in the package, for the route modules.- The same-origin preview with the admin session does not change.
genga.published.loadForRoutestill works and logs the same kind of warning.loadPageof@genga-cms/react-routerreplaces 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
-
Install the adapters.
npm i @genga-cms/react-router @genga-cms/react. They have the version ofgenga. -
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.tsxandapi.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 destructuredloaderfrom the browser build, and the build fails.With a custom
adminPath, for example/cms, useroute('/cms/*', …). With themcpoption, route/mcpand/.well-known/*to the same module. -
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"anddata-themefrom<html>for the admin paths, and the link togenga/admin.css. -
Check your CSP.
genga.handlesets 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. -
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. -
Load the pages with
loadPage. It replacespublished.loadForRouteand thedocumentDatahelper 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;loadPagegives the public shape: media URLs are absolute (they start withsiteUrl), and each rich text value also has itshtml. A component that compares a media URL with a path must change. -
Move the block components out of the config.
defineBlock({ component })still works withRenderBlocksofgenga/client. With@genga-cms/react, the config holds the fields anddefineBlockComponents(gengaConfig, { hero: Hero })gives the components to<Blocks>.usePreviewDataandRichTextof@genga-cms/reacthave the same options as those ofgenga/client. -
Remove
react-routerimports for Genga.genga/serverno longer importsreact-router. If your app used thedata()errors ofgenga.admin.*in its own code, read the plainResponsethat Genga now throws: a 302 withLocation, 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.