perf(core): request the page a link click goes to with the islands runtime

Merged
KI
14d ago
+867-191

Problem

In islands mode a page whose client components all wait starts with the boot script and loads the client runtime on intent. A link clicked before the runtime runs is held and replayed once it does, and only then does the router request the page. The two round trips run one after the other, so on a real network the first tap is much slower than RSC's, which fetches the page right away.

With 150 ms of emulated latency and unlimited bandwidth (HTTP/2, 4x CPU), an 80 ms tap on "Next" after a cold visit:

Sectionrscislands on main
docs200 ms305 ms
shop201 ms342 ms
blog235 ms335 ms
guide232 ms364 ms

A server on the same machine hides this, which is why the existing timing bench showed only a 5 to 66 ms gap.

Root cause

The page request comes from the router, which is part of the runtime the click is waiting for. Nothing requests the page until the runtime has downloaded and run.

Change

  • The boot script requests the held click's page together with the runtime (islands-boot.ts). It sends the headers the router would send from that document: Accept, F-Layouts, F-Render: tree when a hydrating layout root is present (data-farm-root="root") or F-Stream: flush otherwise, and the deployment id. It leaves out F-Values, so the server sends every value the page needs, which is valid for any page.
  • The router's next navigation request takes that response when it would send the same request: same URL without the hash, the same headers except F-Values, and sent within the last 30 seconds (the prefetch cache's window). Anything else aborts the boot's request and sends its own. Prefetches never take it. Aborting the navigation also aborts the boot's request. This is takeFarmEarlyNavigation, shared by the production router (universal-build.ts) and the dev router (vite.ts), and included only in islands mode alongside the stream reader.
  • No new config or public API. FarmEarlyNavigation is @internal.

Safety and compatibility

  • RSC mode is unchanged: the router source includes the helper only when navigations stream (islands).
  • A request that doesn't match is aborted, never rendered. A test covers a different URL, missing F-Layouts, F-Render instead of F-Stream, and a stale request.
  • Costs:
    • The boot script grows from 1.9 to 2.2 KB br.
    • On shop pages, the first navigation's response carries the catalog values again, because its request leaves before the browser holds them. Average shop navigation data goes from 1.7 to 2.0 KB br.
  • redirect: "manual" matches the router's own request, so redirects are handled the same way.

Verification

Results over an emulated network, medians of 5 interleaved rounds, rsc / islands on main / this branch (benchmarks/server-components-payload/first-navigation.mjs, Apple M1, Chrome 157, 4x CPU, HTTP/2 proxy):

Section150 ms, 80 ms tap150 ms, 100 ms hover50 ms, instant tap50 ms, 100 ms hover
docs200 / 305 / 184183 / 241 / 181113 / 212 / 14689 / 45 / 46
shop201 / 342 / 215183 / 289 / 217114 / 247 / 19585 / 64 / 64
blog235 / 335 / 214227 / 276 / 215148 / 241 / 181133 / 73 / 64
guide232 / 364 / 241224 / 310 / 235147 / 263 / 214131 / 86 / 84
  • At 150 ms with unlimited bandwidth, a first tap or a short hover-then-click is now faster than RSC on docs and blog, and 9 to 34 ms slower under a hydrating layout (shop, guide), where main is 86 to 141 ms slower.
  • An instant tap at 50 ms is still 33 to 81 ms slower than RSC, because the runtime has to start (and hydrate the layout on shop and guide) before the page renders.
  • A 100 ms hover at 50 ms is unchanged (the prefetch already wins there).
  • A rerun under heavier load (load average 7 to 9) put the engines within about 25 ms of each other either way at 150 ms.
  • This does not close the slow-connection gap. At 1.6 Mbit/s and 150 ms, the runtime and router (about 91 KB br) arrive with the first tap, so it is 434 to 570 ms slower than RSC on every section (samples within a few ms of each other): docs 264 / 698, shop 220 / 790, blog 286 / 773, guide 286 / 827. The docs now say so.
  • The bench proxy brotli-compresses text responses the server sent uncompressed, as a CDN would, because the rsc engine's Node server doesn't compress its pages or Flight responses (islands does). Without that, an RSC blog navigation takes over a second at 1.6 Mbit/s. This is a separate bug, not fixed here.

Commands:

  • pnpm --filter @farm.js/core exec vitest run src/__tests__/islands-boot.test.ts src/__tests__/islands-early-navigation.test.ts: 27 passed
  • pnpm --filter @farm.js/core type-check: passed
  • FARM_SERVER_COMPONENTS_MODE=islands pnpm exec playwright test --config playwright.server-components.config.ts: 139 passed, 8 skipped
  • Same with FARM_SERVER_COMPONENTS_SERVER=dev: 136 passed, 11 skipped
  • FARM_SERVER_COMPONENTS_MODE=rsc pnpm exec playwright test --config playwright.server-components.config.ts: 119 passed, 27 skipped, 1 failed. The failure is environmental: the disk filled during the run, and "an inline server action works before JavaScript loads" failed in context.close() with ENOSPC after its assertions passed. I couldn't rerun locally (the worktree's dependencies were cleaned up to free disk); CI's test:e2e:framework runs this suite
  • FARM_SERVER_COMPONENTS_CSP=nonce FARM_SERVER_COMPONENTS_MODE=islands …: 4 passed
  • node benchmarks/server-components-payload/measure.mjs: sizes in results/latest.json
  • Regression: new e2e test "a link followed before the client runtime loads requests its page with the click". It holds the runtime and expects exactly one request for the page while it is held. I built core with main's islands-boot.ts, and it failed at that poll with 0 requests. With the change it passes.

Not verified:

  • A clean local RSC run (prod or dev), the Cloudflare and Vercel targets, and the client-plugins variants: left to CI's test:e2e:framework
  • Windows
  • The path where a "visible" or "idle" island loads the runtime before the click (the early request isn't made there, since the router is already running)
  • Real devices or real mobile networks (emulated latency only)

Documentation and release

  • docs/server-rendering/islands: describes the behavior and updates the comparison table (2.2 KB boot script, shop navigation data, new "First tap, 150 ms latency" and "First tap, at 1.6 Mbit/s" rows). The first-navigation caveat now gives both, including the slow-connection loss.
  • docs/server-rendering: 1.9 → 2.2 KB.
  • benchmarks/server-components-payload: new first-navigation.mjs (with --down <kbit/s> and CDN-like compression in its proxy), README section with method and table including 1.6 Mbit/s, regenerated results/latest.json.
  • No package or release-flow changes.

Summary by cubic

Speeds up the first client navigation from an islands page by requesting the clicked page at click time, in parallel with the runtime, instead of holding the click until the runtime runs and only then fetching. Over an emulated 150 ms link a first tap now takes 184–241 ms instead of 305–364 ms and beats RSC's 200–235 ms on pages without a hydrating layout, at the cost of a 1.9 → 2.2 KB br boot script and a first shop navigation that again carries the catalog values (1.7 → 2.0 KB br).

Behavior and safety

  • The boot script sends the page request with the headers the router would send from that document, minus the held island values (F-Values), so the response suits any page.
  • takeFarmEarlyNavigation (prod and dev routers, islands mode only) reuses the response when the navigation request matches: same URL sans hash, same headers except F-Values, within 30 s. Anything else aborts the early request; prefetches never take it. RSC mode is unchanged.
  • Redirects behave as before, since the early request uses redirect: "manual" like the router's own.
  • The benchmark proxy compresses a text response the server left uncompressed, as a CDN would; the docs note the rsc engine's Node server does so after #3053.

Verification

  • Unit tests cover matching and mismatched early requests (URL, missing F-Layouts, F-Render vs F-Stream, stale), and a source check confirms the router builds its request headers once, from createFarmDeploymentRequestHeaders, so they can be matched against the boot's request.
  • A new e2e test holds the runtime and asserts exactly one page request is made while it is held.
  • New benchmark first-navigation.mjs measures the first navigation over an emulated network (HTTP/2 proxy, CDP latency and CPU throttling, optional bandwidth limit); docs and results/latest.json updated.