fix(core): read the request's path in the renderer router store on the server

Merged
KI
12d ago
+79-3

Problem

With Solid, Vue, Svelte, or a custom renderer, a component that renders the router's path or params renders / on the server, whatever the URL. The browser then renders the real path, so hydration disagrees with the server's markup, and a page that doesn't hydrate keeps the wrong value.

Reproduction: a page at /crops/wheat that renders useRouter().pathname sends path: / in its HTML.

Root cause

createRendererRouter() in @farm.js/core/renderer-client returned a fixed snapshot whenever window was undefined: pathname /, empty search params, no params. The Solid (farm-solid/src/bindings.ts:57), Vue (farm-vue/src/bindings.ts:72), and Svelte bindings and custom renderers build their router on this store. React's useRouter() (client/router.ts) had the same bug and reads the current request through _peekCurrentRequest() since its own fix. The renderer-neutral store was written separately and never got that fix. No test rendered the store without a window inside a request.

Change

On the server, readRendererRouterSnapshot() reads the current request's URL through the same request bridge React's router uses. Then it applies the existing logic: strip the base path, strip the locale for param matching, and match routes. Outside a request it still returns the site root. pageState stays null on the server. The browser path is unchanged. server/request-bridge.ts has no imports, so the browser bundle gains only the small function.

Safety and compatibility

  • Browser behavior: unchanged.
  • Server behavior: the snapshot now matches what the browser reads on hydration. Code that relied on the server always seeing / would change, but that was the bug.
  • Every non-React renderer's router binding is covered through the shared store. React already behaved this way.

Verification

  • packages/farm: vitest run src/__tests__/renderer-router-server-url.test.ts covers path and query, params, base path, and outside a request. On main, the two in-request cases fail with expected '/' to be '/users/42'. With the fix, they pass.
  • The same run with renderer-client.test.ts, navigation-ssr-request-url.test.tsx, and router-parity.test.ts: 27 passed.
  • examples/custom-renderer: the crop page renders useRouter().pathname from a page that doesn't hydrate. The new browser test "the router reads the request's path while the server renders a page" checks the server HTML:
    • With the fix: 9/9 pass in farm dev and 9/9 in a production build (local Chrome).
    • With main's renderer-client.ts rebuilt into core: the new test fails in both, and the HTML contains path: /.
  • tsc --noEmit in packages/farm: exit 0.
  • oxlint and oxfmt --check on the changed files: clean.

Not verified locally:

  • The Vue, Solid, and Svelte suites only partly ran here. Their failing files can't load unbuilt @farm.js/renderer-tests and @farm.js/svelte dists in this worktree; every test that loaded passed. CI runs them in full.
  • The example's tsc needs core's .d.ts build, which I didn't run.

Documentation and release

None on main: no page documents the old server snapshot. The new custom renderers page in #3127 is updated to describe this behavior.


Summary by cubic

Fixes the renderer router store so Solid, Vue, Svelte, and custom renderers read the request's path on the server instead of always rendering /, which caused hydration mismatches on pages that don't hydrate.

Now createRendererRouter() reads the current request's URL through the same request bridge React's useRouter() uses, applies the existing base-path and locale stripping, and matches routes. Outside a request it still falls back to the site root, and pageState stays null on the server. Browser behavior is unchanged.

  • Adds a regression test covering path, query, params, and base path in a node environment, plus a custom-renderer example page and an e2e test asserting the server HTML.