perf(islands): stop resending values a page holds before islx loads

Merged
KI
13d ago
+515-195

Follows #3071 (merged) and answers RFC 0011's open question (#3064).

Problem

Until islx loads, a navigation in islands mode names no island values (F-Values), so the server sends again every value the page already holds. #3071 made this the common case under a layout that hydrates as one root: the layout keeps waiting through navigations, so islx does not load, and every navigation on the benchmark's shop section carries the shared catalog again. That made shop navigations 6.0 KB br against RSC's 3.8 KB.

Root cause

The router named held values only from islx's store (getFarmIslandValueHeaders), which exists once islx's client runtime has loaded. Before then the page's <script type="f/value"> elements are the only copy of its values, and a swap replaced them with the next page's, so the router could not name them without losing values the server would then leave out.

Change

Before islx loads, the page's value scripts count as held:

  • The router names the value scripts under the root (the latest 128, as the store does) when there is no store. So does the boot's request for a held click.
  • The router's swap keeps them (keepFarmIslandValues). With islx's store it ingests the old content before the swap. Without it, it puts the value scripts the new content lacks back into it: right before the last server tree's payload, joining the run of value scripts that tree renders again on hydration (server-tree.ts), or else at the end of a page React does not hydrate node by node, or else in the body.
  • The production router (universal-build.ts) and farm dev's document navigations (vite.ts) do both.

The boot imports nothing at runtime, so it carries a three-line copy; a test checks that it names the same keys as getFarmIslandValueHeaders.

No new config or public API.

Safety and compatibility

  • Islands mode only: the header and the keep step are generated only where island values are on. RSC builds and the server are unchanged.
  • The server already treats F-Values as a hint it validates: invalid keys are dropped, and shared-cacheable renders ignore it.
  • A response for fewer held values fits any page: the boot's early request names what the DOM holds, and the router's swap keeps at least that.
  • The value scripts a swap carries are bounded: the latest 128 keys, the ones the request named.
  • A carried script never adds markup React did not render: the last tree re-renders the run of value scripts before its payload (server-tree.ts, and islx's layout trees), a page without a tree is server HTML (islands hydrate in their own f-island roots), and a client page's go in the body, outside any React root.
  • Requests before islx loads now carry F-Values, as requests after it already did, so the server and caches see nothing new.
  • farm dev asks for some pages' data through its page-data endpoint first (a waiting root's pages, for one). That request names only what islx holds, so those navigations carry values as before.

Verification

Bytes, node benchmarks/server-components-payload/measure.mjs (brotli 5, ten navigations through "Next", Apple M1, macOS 26.2, Node 24.21.0, Chrome 157; 2026-11-22):

SectionNavigation data, br (raw) beforeAfterFirst-load JS, brNavigation JS, br
shop6.0 KB (30.9 KB)4.4 KB (19.9 KB)2.2 KB, unchanged2.7 KB, unchanged
docs, blog, guide2.7, 9.2, 9.6 KBunchanged2.2 KB, unchangedunchanged

RSC on shop is 3.8 KB. About three quarters of the 4.4 KB is the page's own HTML, which a waiting root needs to swap the page in without React DOM (a server request for /shop/s1 naming /shop/s0's values: 4.46 KB br, 3.37 KB of it the <article>). docs, blog and guide don't change: docs' values sit in the layout the browser keeps (F-Layouts), blog has none, and guide's one value differs per post.

Bundle (brotli 11, bench build): boot 2,074 to 2,132 B; the router's shared chunk with the value protocol 123 to 437 B; router 9,979 to 10,014 B; client entry 12,546 to 12,547 B. The RSC build's output does not contain the new code.

Time is not claimed. From bytes alone, a shop navigation at 1.6 Mbit/s should save about 8 ms, and the first navigation or interaction on docs and blog pays about 2 ms for the 370 B of new JavaScript, both well under first-navigation.mjs's run-to-run noise. Not measured.

Tests:

  • pnpm --filter @farm.js/core exec vitest run src/__tests__/island-value-keep.test.ts src/__tests__/islands-boot.test.ts src/__tests__/islands-early-navigation.test.ts src/__tests__/island-values.test.tsx: 4 files, 51 tests passed. New: naming the page's value scripts (latest 128, each once), where the swap puts the ones the next page lacks (a tree's run, a page without a tree, a client page), no duplicates, and leaving it to islx's store once loaded. The boot's request names the same keys getFarmIslandValueHeaders does.
  • Two new contract tests in tests/e2e-server-components/contract.spec.ts: a navigation before islx loads between pages that pass the same list to an island that waits (/values/held/a to b), and out of a layout root that waits (/context/wait/a to b). Each checks that the requests name the page's values and their responses leave them out, then hydrates the next page and reads the list. With the swap's keeping disabled (the request still names the values), both fail: the island or root never hydrates ("40 entries").
  • FARM_SERVER_COMPONENTS_MODE=islands pnpm exec playwright test -c playwright.server-components.config.ts (production): 144 passed, 8 skipped.
  • Same with FARM_SERVER_COMPONENTS_SERVER=dev: 138 passed, 14 skipped. In an earlier full dev run, "a promise passed to a client component streams in, and the component works before it" failed once on a first load this change doesn't touch; it passed 3 of 3 alone and in both later full runs.
  • FARM_SERVER_COMPONENTS_MODE=rsc (production): 120 passed, 32 skipped (the two new tests are islands-only).
  • pnpm --filter @farm.js/core test: 744 files passed, 1 skipped (6,820 tests passed, 10 skipped).
  • pnpm --filter @farm.js/core type-check: clean. pnpm exec oxlint and pnpm exec oxfmt --check on the changed files: clean.

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

Documentation and release

  • docs/src/app/docs/server-rendering/islands/page.md: the shop navigation-data cell and the caveat about navigations under a waiting root.
  • benchmarks/server-components-payload/README.md and results/latest.json: the new bytes.
  • examples/server-components: the shared list on the waiting pages, and /values/held/[item], with its generated route types.
  • No changeset: Farm releases with bumpp.