feat(core): let a renderer recover from hydration errors in production

Merged
KI
12d ago
+294-17

Problem

When hydrateRoot() throws, development logs the error and renders the route with createRoot(), so the page works. A production build has no fallback: the page keeps the server's markup with no event listeners. Reproduced with leaf on 0.1.81: a page whose first browser render throws is interactive in farm dev and dead after farm build.

Not every renderer should get the fallback in production, though. It replaces the page with a fresh client render, and that is only safe when the renderer's createRoot() replaces the server markup the failed hydration leaves in the container. Today that's true for React, Vue, Solid, and Svelte, but not for Preact or leaf, where the page would show up twice.

Change

  • New renderer capability, capabilities.hydrationFallback. Off by default.
    • Off (every official renderer, including React): the generated production entry is the same as on main, so production behavior doesn't change.
    • On: production's hydrateFarmRoute() catches the throw, logs [Farm.js] Hydration error; rendering the page on the client instead: as development does, and renders the route with createRoot().
  • Development is unchanged for every renderer.
  • leaf opts in. farm-leaf's createRoot() now empties its container first, so the fallback replaces the server markup instead of rendering next to it.
  • examples/custom-renderer gains /recovers, a page whose first browser render throws.
capabilities: {
  hydrationFallback: true, // production renders the route again when hydrateRoot() throws
},

Safety and compatibility

  • Additive. The new optional field defaults to false, and no official renderer's descriptor or runtime changes.
  • @farm.js/renderer-tests only gains the optional key in its descriptor fixture type.
  • Development keeps its fallback for every renderer, so for renderers that don't opt in, dev and production still differ as they do today.
  • Preact's createRoot() keeps markup it can't match. A shared conformance check fails for Preact the same way it fails for leaf, so Preact should not opt in until its adapter changes. That is left for a separate PR.

Verification

  • vitest run src/__tests__/production-hydration-fallback.test.ts runs the emitted hydrateFarmRoute() against a throwing hydrateRoot(). 2 passed.
    • On main, the opt-in case fails.
    • On the first commit of this PR (fallback for everyone), the "React and renderers that don't opt in are left alone" case fails.
  • Custom-renderer e2e, Chrome channel, with the new a page whose hydration throws is rendered in the browser instead test:
    • production: 12 passed (that test failed with core built from main)
    • dev (FARM_CUSTOM_RENDERER_SERVER=dev): 12 passed
  • pnpm --filter farm-leaf test: 26 passed. The new createRoot() test fails without the adapter change (<span>Client render</span><p>Server markup</p>).
  • Core tests (renderer, production-prebuilt-ssr, client-component, dev-docs-layout-hydration, hydration-server-props, islands-client-entry): 257 passed.
  • renderer.test.ts in farm-react, farm-preact, farm-solid, farm-vue, and farm-svelte: all pass (19, 20, 29, 26, 24).
  • pnpm --filter @farm.js/core type-check and pnpm --filter @farm.js/renderer-tests type-check: clean.
  • farm generate --check in the example: up to date.
  • oxfmt --check and oxlint on changed files: clean.

Not run: pnpm docs:build, Windows.

Documentation and release

  • docs/renderers has a new "Hydration errors" section.
  • docs/renderers/custom and the bring-your-own-renderer blog post show the capability and the createRoot() wrapper.
  • The standalone farming-labs/leaf needs the same two changes to opt in.