Server rendering
Three kinds of page are rendered on the server before the browser runs any JavaScript, and then hydrated in place:
| Route | Data the server fetches | Rendered for |
|---|---|---|
/hub | fetchPublishedLessons() | Everyone |
/hub/:id | fetchLesson(id) | Everyone |
/hub/:id/<tab> | fetchLesson(id) | Everyone |
/hub/:id/discussion | fetchLesson(id) + fetchComments(id) | Everyone |
/users/:id | fetchUserProfile(id) | Everyone |
Only the discussion tab fetches comments, because it is the only view that shows them. They seed its thread, so the comments are in the served HTML rather than behind a skeleton, and they feed the comment thread in the lesson's JSON-LD on that tab alone: structured data has to describe what a reader of the page can see. The list is the public one, the same for every reader of a public lesson, so the browser doesn't fetch it again after hydration. A failed comments fetch costs only the server-rendered thread and its markup (the tab then loads them itself), never the render.
Everything else (/, /editor, /library, /settings, /login, /oauth/authorize, /moderation) is served as the static SPA shell.
Both hosts do this: the Cloudflare Worker (handleFrontend in apps/api/src/routes/render.js) and the self-hosted Node entry (apps/api/src/node/server.js) call the same shouldServerRender / serverRender pair from apps/api/src/routes/ssr.js.
The data is loaded the way the browser loads it, through core's own client, from the origin the page is being served at. Neither host can reach that origin over the network from inside itself. Cloudflare does not route a Worker's fetch to its own hostname back into the Worker; it sends it on to the zone's origin server, and this site has no origin server behind the Worker, so the request dies as a 522. On the Node host behind the compose stack's gateway, the public address names a port only the gateway listens on. So both hosts answer any request to the origin they are currently serving in-process, with the app's own handler, and only send other origins over the network. That is apps/api/src/lib/selfFetch.js, which the Worker installs in src/index.js and the Node entry in src/node/server.js. Without it, every render fails with "SSR data/shell failed" and falls back to the shell. The hosted MCP server's tools hit the same wall from the other side: they call the API from the HubMcp Durable Object, where no Worker request is being served, so they register the API origin as a standing route that is always answered in-process (see Remote mode). The one exception is /rest/v1 and /auth/v1, which the app never serves: on an origin that also fronts PostgREST and GoTrue, those still go over the network to them.
A lesson's tabs (/hub/:id/practice, /discussion, /proposals, /proposals/:prId, /history; see Pages & routing) are all the same lesson, so one fetchLesson serves all of them and the tab decides what to draw with it. LESSON_PATH names those forms rather than matching any extra segment, so it and the SPA's route table agree about what a lesson URL is: a wildcard would let /hub/:id/anything through, costing a lesson fetch and a full render to produce a page that isn't one. Adding a tab means editing both. The pattern itself lives in apps/api/src/routes/spa.js, the server's copy of the route table, and ssr.js imports it (with HUB_PATH and PROFILE_PATH), so the code deciding a path is a lesson and the code deciding it is a page at all cannot disagree. LessonLayout seeds its state from that payload and the tab reads it through useLesson(), so no tab fetches the lesson a second time.
Matching the tabs is not optional. The service worker's navigateFallback denylist excludes the whole of /hub/* from the precached shell (WORKER_PATHS in apps/web/vite.config.js) precisely because the server answers those paths itself. A tab that this route failed to match would be excluded from the shell and left unrendered: online it falls through to the SPA shell and still works, offline there is nothing to fall through to and it fails outright. The two lists have to stay in step, and both say so in a comment.
The bootstrap payload is keyed by the exact url.pathname the server rendered, and useServerData only hands it to a page still on that path, so following a tab link releases it and the layout keeps serving its own state, which is the behavior you want.
Why only these three
Because their primary content is a public read. The Supabase session lives in localStorage behind PKCE and is invisible to a server, so the render is always anonymous; the personalized parts of the page (the account menu, an author's Edit/Delete controls, whether you follow a profile) fill in when the client hydrates. No session ever has to move to a cookie, which is what keeps this tractable. A private draft 404s on the server for the same reason, and its author sees it after hydration, when their own authenticated fetch runs.
fetchLatestLessons and fetchUserActivity can't be server-rendered at all: they parse Atom with DOMParser, and live in @spelling-creator/core/browser/feeds. Both are dashboard content, so hydrating them is correct.
How it fits together
apps/web/src/entry-server.jsx the app, built for workerd (`build:ssr` -> dist-ssr/)
apps/web/src/lib/ssr.jsx the client/server handoff (SsrProvider, useServerData, useSiteOrigin)
apps/api/src/routes/ssr.js the server route: match, fetch, render, splice
apps/api/src/routes/spa.js the route table, and the asset/shell/404 fall-through- The frontend handler asks
shouldServerRender: aGETwhoseAcceptincludestext/html, to one of the routes above, without the prerender browser's__prerenderflag. serverRenderconfigures core from the server's own bindings (sohasSupabase()agrees with what the client will compute), fetches the page's data (through the very same@spelling-creator/coremodules the browser calls, so the two paths can't drift) and callsrender()from the server bundle.render()returns{ head, body }. React hoists<title>/<meta>to the front of its output when rendering a subtree rather than a whole document, so they're split off and spliced into the real<head>; a scraper won't read anog:tag it finds in the body. The stream is read to the end rather than streamed out, so the status and the head are settled before a byte is sent.- The server injects the body into
<div id="root">and serializes the data intowindow.__SSR__. The response is sent withCache-Control: no-cacheand anX-Rendered: ssrheader, which is the quickest way to tell a rendered page from the plain shell. src/main.jsxreads that, deletes it fromwindow, and callshydrateRootinstead ofcreateRoot.
Failure at request time is always soft. A failed data fetch or a render error falls through to the static shell, which is what the app served before any of this existed. A missing server bundle is not a runtime case: ssr.js imports it statically, so the Worker fails to bundle without it, which is why every script that reaches wrangler builds it first.
The one failure treated as an answer rather than a breakdown is a 404 from the API: core's fetch errors carry the HTTP status, so /hub/<deleted-id> is answered with the shell under a real 404 instead of a page insisting forever that it exists (a "soft 404", which is what gets junk URLs indexed). A private draft takes the same path on purpose: the render is anonymous, the API reports a draft exactly like a missing lesson, and the SPA never reads the status, so the draft's author still reaches it after hydration through their own authenticated fetch. A path that isn't a route at all is still 404ed outright, without a render (Unknown paths).
That only works if the API's 404 means "missing" and nothing else, so the API keeps two promises. GET /lessons/:id answers an id that isn't a UUID with a 404 straight away, since PostgREST would reject it with a 400 that surfaced as a 502 and a soft 200. And GET /profiles/:id answers a lookup that failed (GoTrue unreachable, a 5xx, rate limiting, a bad service key) with a 502, not the 404 it used to give, so an auth outage can't tell crawlers that every profile is gone (lookupPublicUser in apps/api/src/lib/supabase.js).
Page metadata
src/lib/seo.jsx exports a <DocumentMeta> component, not a hook. React 19 hoists <title>, <meta> and <link> into <head> from anywhere in the tree, so page metadata is ordinary JSX, which is exactly what makes it work under SSR, where an effect that writes into document.head never runs.
Every page renders one. An indexable page carries a canonical link and a matching og:url, without a trailing slash, since the routes accept one and the sitemap lists none. A lesson's tabs all point theirs at /hub/:id, so the views of one lesson don't compete with each other in search results. The signed-in and tool pages (the editor, library, settings, login, moderation and the OAuth consent screen) pass noindex instead, since their content is the visitor's own.
JSON-LD is deliberately not hoisted: React only hoists <script> when it's async, which is meaningless for a non-executable type. <JsonLd> renders in place, which search engines accept. The same file builds the structured data: buildLessonSchema (a lesson page, typed Article + LearningResource, with its pictures' credits and its comment thread), buildLessonListSchema (the hub's ItemList of plain LearningResources), buildProfileSchema (a profile's ProfilePage, whose Person carries the @id that lesson markup points its author at), buildBreadcrumbSchema (a lesson page's visible crumb trail, which desktop results can show in place of the raw URL) and buildWebSiteSchema (the homepage's WebSite, which is where search engines read the site name). All of them name the site through one shared Organization, identified across pages by its @id.
Things that had to change to make it work
index.html's site-wide social defaults are stripped on a rendered page (its<title>,description,og:*andtwitter:*tags), before the page's own tags go in. Otherwise a scraper reading the firstog:titlewould get the generic one. The default tags carry adata-seo-defaultmarker and are removed by it, because the Chromium prerenderer removes the same set from a live DOM in which the page's own React-hoisted tags already sit, where matching by tag name would take the page's tags too.- The service worker must not answer these navigations.
navigateFallbackwould otherwise serve the precached shell and the server would never be asked, silently disabling SSR for exactly the returning visitors whose browsers have the shell cached. The routes are inWORKER_PATHSinapps/web/vite.config.js; see Installable app & offline use. ColorSchemeProviderdoesn't readlocalStorage/matchMediaduring render. The server can't, and a hydrating client has to render the same thing the server did: the theme toggle shows a sun or a moon, so this is markup, not just a CSS variable. The stored choice is adopted in a layout effect, after hydration but before paint. Page colors were already correct before paint via the inline script inindex.html.RichTextsanitizes only in the browser. DOMPurify needs a real DOM, so the server (and the hydrating pass) render a rich-text value as its plain text, throughrichTextToPlain, and the formatted version replaces it immediately after mount. Plain text is safe unsanitized because React writes it as a text node, and it puts a bio's or a comment thread's words in the served HTML. Rendering the unsanitized HTML server-side is not an alternative: React never re-checksdangerouslySetInnerHTMLduring hydration, so whatever the server wrote is what the reader keeps.- Comment timestamps fill in after hydration. They are formatted in the reader's own locale and time zone, which the server can't know, so both the server and the hydrating pass leave them blank rather than disagree.
- Translations get an instance per render.
entry-server.jsxcreates a fresh i18next instance frombaseConfiginsrc/lib/i18n.jswithlng: "en", because module scope in a Worker is shared by concurrent requests. See Internationalization. src/main.jsxandsrc/entry-server.jsxmust stay structurally in step. Anything that renders DOM has to appear in both, in the same order. Sonner's<Toaster>renders an empty<section>even with no toasts, so it's in both;ServiceWorkerPromptrendersnulland is browser-only, so it isn't.- Splicing uses function replacements. Everything spliced into the shell carries user text, and in a string replacement
$'and friends are substitution syntax, so a lesson titled$'would duplicate the rest of the page.
What SSR replaced, and what it didn't
apps/api/src/routes/render.js uses headless Chromium (the BROWSER binding in apps/api/wrangler.jsonc) for two unrelated jobs:
prerender(): an HTML snapshot for about 30 crawler user-agents (CRAWLER_UA). SSR replaces this for the routes above. It's still used for/(whose content is auth-gated, so an anonymous render is only ever the marketing splash) and for any SSR attempt that fails. The Node host has no Chromium and skips this step.ogImage(): live 1200x630 screenshots for link previews. Unaffected. SSR cannot take a screenshot, so the binding stays.
Build order
The server imports a build artifact, so apps/web must be built before the Worker is bundled:
pnpm build # client -> apps/web/dist, server -> apps/web/dist-ssr
pnpm run deploy # runs the above, then the docs build, then the API's deployapps/web's build script is the client only and build:ssr is the server. In turbo.json, the API's test task depends on build:ssr and its bundle task on both, and pnpm dev:api and pnpm run deploy build both before starting wrangler (see Turborepo). In the Workers test project, the pool runs against a test-only entry (apps/api/src/collab-room.test-worker.js) that doesn't import the route table; the Node tests do import ssr.js, which is why the bundle has to exist.
The server build stubs out modules it can never run. SSR_UNREACHABLE in apps/web/vite.config.js lists them: the export engine, the git engine, the editor, moderation, login and OAuth pages, and the on-device model engines (translation, summary, import and read-aloud). They are all reached only through a dynamic import(), so the stub never has named exports to fail to provide; reaching one at render time throws, which means the route table and that list disagree.
Local development
pnpm dev:web (the Vite dev server) does not server-render; there is no Worker in front of it, so every route arrives as the plain SPA shell and mounts with createRoot. To exercise SSR, run pnpm dev:api. It builds both halves of the web app (replayed from the Turborepo cache when nothing changed), then serves the built assets in apps/web/dist through the real Worker. It does not watch apps/web, so run it again after changing the app.