perf(islands): keep a layout root waiting through its first navigation

Merged
KI
14d ago
+506-505

Implements RFC 0011 (#3064).

Problem

RFC 0010 took React DOM off a first navigation's path when the next page streams in (docs, blog). A route whose layout hydrates as one React root (a client provider around the pages, as on the benchmark's shop and guide sections) still waited for it. At 150 ms latency and 1.6 Mbit/s, an 80 ms first tap there took 788 and 837 ms against RSC's 223 and 306. This brings it to 350 and 397.

Root cause

In islands mode such a root waits for an interaction (RFC 0003, section 11), but the production router treated a navigation out of it as a render in it:

  • It asked for the next page as a tree (F-Render: tree) and rendered it in a new React root, so the navigation waited for React DOM, islx and the root's modules (about 70 KB br).
  • Hovering a link in the root hydrated it (#3033), so the navigation could update the root instead of creating it.
  • After swapping in a page under such a layout, it hydrated the root at once, whatever the route's strategy.

The dev router already swapped the page in and scheduled the root by its strategy. Before its trigger, the root holds no client state: its DOM is server HTML, and what it hydrates from is the tree script the document carries. A whole navigation response for the same route carries the same.

Change

While no React root owns the page's root, a navigation lands the next page the way a first load of it would:

  • The router asks for the next page whole, not as a tree. The boot's request for a held click does the same. The router takes a whole response for a request it would stream, since a response without flush markers is all shell.
  • swapContent swaps the page in and schedules the root by the destination route's strategy (scheduleFarmRouteHydration, now shared with the first load). A root strategy of load hydrates at once, as before.
  • Hovering a link no longer hydrates the root (removed from the production and dev entries). Pressing or focusing a link no longer starts hydrating the island or root around it either; the scheduler already let a link's click through.
  • The boot no longer preloads the root's modules or React DOM for a link's intent, only for another interaction in the root.
  • The entry no longer loads React DOM and islx when it starts. What first renders or hydrates loads them, as the islands runtime facade and createMatchedHydrationRoute already did.
  • The payload-only branch for a root that never hydrated is gone: a tree response now comes only for a root React owns.

No new config or public API.

Safety and compatibility

  • Only islands mode's production router and entry change, plus the scheduler that islands and roots share. RSC builds, the mobile client and the dev router's swap are unchanged. Dev loses its hover hydration, as production does.
  • A root that never hydrated holds no state to lose. After the swap it is the document the server sends for the destination, so its later hydration is that page's first load. Once it has hydrated, navigations render in it as before and keep the provider's state (contract test).
  • A root strategy of load still hydrates as soon as the page swaps in.
  • Behavior change: a link a client component renders inside a container that waits no longer gets the chance to hydrate it first, on hover in a root or on press in an island or root. Its click navigates without running its onClick, as a tap already did, because neither the boot nor the scheduler holds a link's click.
  • Data per navigation: until the root hydrates, a navigation carries the page's HTML as well as its tree, and, before islx runs, the values the browser already holds (the router names held values from islx's store, which does not exist yet). shop: 2.0 to 6.0 KB br per navigation (5.2 to 30.9 KB raw); guide: 9.5 to 9.6 KB. The JavaScript per navigation falls by more: 9.7 to 2.7 KB br on both, averaged over ten navigations (measure.mjs). Naming the values the document holds before islx runs is a follow-up; it applies to every islands navigation, not only this one.
  • Bundle: boot 2,098 to 2,074 B br, client entry 12,590 to 12,546 B, router chunk 9,973 to 9,979 B (brotli 11, bench build). First-load JavaScript is unchanged at 2.2 KB.

Verification

benchmarks/server-components-payload/first-navigation.mjs, interleaved per round: rsc / islands at df6c467af (this branch's base) / this branch. Medians in ms; HTTP/2 through the bench's TLS proxy, 4x CPU; Apple M1, macOS 26.2, Node 24.21.0, Chrome 157; 2026-11-22, load average 7 to 71 (other sessions ran tests). Medians of 5, of 9 for the two 150 ms columns without a bandwidth limit.

Section150 ms at 1.6 Mbit/s, 80 ms tap150 ms, 80 ms tap50 ms, instant tap50 ms, 100 ms hover150 ms, 100 ms hover
docs267 / 317 / 316246 / 217 / 211124 / 115 / 12185 / 31 / 38199 / 205 / 193
shop223 / 788 / 350247 / 241 / 213111 / 203 / 12687 / 60 / 47197 / 190 / 193
blog303 / 390 / 379269 / 227 / 218162 / 168 / 165139 / 66 / 67231 / 189 / 221
guide306 / 837 / 397273 / 251 / 235158 / 221 / 170134 / 97 / 77232 / 197 / 212
  • The first run of the 150 ms, 80 ms tap column (at load ~70) stopped on one visit where the page loaded a document mid-measurement; the script does not record which engine. Reruns with per-visit logging found none in 156 visits: the column above (108 visits, load 7 to 13) and a 12x CPU run of both islands builds (48 visits; shop 543 to 292 ms, guide 559 to 408 ms).

  • Same-machine timing.mjs (engines one after another, not interleaved) moved every engine by up to 2x between two runs, so the docs table's first-navigation row now uses the interleaved 50 ms hover column. In both runs the first navigation on shop and guide fell (shop 91 to 61 and 79 to 45 ms, guide 92 to 68 and 87 to 74 ms).

  • pnpm --filter @farm.js/core exec vitest run src/__tests__/islands-boot.test.ts src/__tests__/island-non-click-activation.test.ts src/__tests__/islands-early-navigation.test.ts: pass. The new scheduler and early-navigation tests fail without their fixes.

  • pnpm --filter @farm.js/core test: 691 files passed, 51 failed only because workspace packages had no dist in this fresh worktree (Failed to resolve entry for package "@farm.js/stripe" and similar) plus one islands build timing out at 120 s under load. After building those packages, vitest run on the 51 files: 51 passed (387 tests).

  • pnpm --filter @farm.js/core type-check: clean. pnpm exec oxlint and pnpm exec oxfmt --check on the changed files: clean.

  • FARM_SERVER_COMPONENTS_MODE=islands pnpm exec playwright test -c playwright.server-components.config.ts (production): 142 passed, 8 skipped.

  • Same with FARM_SERVER_COMPONENTS_SERVER=dev: 136 passed, 14 skipped.

  • Same with FARM_SERVER_COMPONENTS_MODE=rsc (production): 120 passed, 30 skipped.

  • The four rewritten contract tests against origin/main's core (df6c467af), production islands: 4 failed, each at the behavior this changes (the next page waits for React DOM; the request asks for f-render: tree; hover hydrates the root; the navigation fetches the root's modules).

Not verified: Windows and Linux (CI), deploy targets other than the Node server, the framework e2e suite (pnpm test:e2e:framework, CI), and pnpm docs:build (the docs change is prose and table cells in an existing page).

Documentation and release

  • docs/src/app/docs/server-rendering/islands/page.md: the browser and layout bullets, the comparison rows and the caveats.
  • benchmarks/server-components-payload/README.md and results/latest.json: the new measurements.
  • RFC 0011 is in #3064, updated to this design.
  • No changeset: Farm releases with bumpp.

Summary by cubic

Keeps a layout root that waits for an interaction waiting through its first navigation in islands mode. Previously, navigating out of such a root waited for React DOM and the root's modules (about 70 KB); now the router swaps the next page's HTML in and schedules the root by the destination route's strategy, so the navigation doesn't block on the renderer.

  • Hovering a link no longer hydrates the root, and pressing or focusing a link no longer starts hydration of the island or root around it.
  • The boot no longer preloads root modules or React DOM for a link.
  • Until the root hydrates, each navigation carries the page's HTML as well as its tree, so data per navigation rises (shop: 2.0 to 6.0 KB br), but navigation JavaScript falls from 9.7 to 2.7 KB br.
  • At 150 ms latency and 1.6 Mbit/s, a first 80 ms tap drops from 788 and 837 ms to 350 and 397 ms on shop and guide.
  • Link clicks inside a waiting container now navigate without running their onClick if the container never triggered hydration, matching the behavior of a tap.
  • No new config or public API; dev loses hover hydration along with production.