perf(react): load the reviver and action client only when a page uses them
Problem
On a page whose islands take plain JSON props, the browser still loads all of islx's client code with the first island: the code that revives slots, server trees, actions, promises, streams and tagged values, plus the action call path, the value store and the codec. On the dash page of Farm's islands benchmark (24 widgets, all plain props), islx's first-load code is 13.2 KB brotli. React Server Components' entry plus Flight is about 13.9 KB. With Farm's own code on top, islands mode is still about 45 ms behind rsc to the moment every component has hydrated at 1.6 Mbit/s.
Root cause
Two things kept that code in the first load:
hydrate.tsimported the reviver,createSlotTree(tree.ts),createServerReference(actions.ts), the value store and the codec statically.forms.tsimportedcreateServerReferencestatically, andisland.ts(where the React contexts lived) importstree.ts.- The dist bundled each entry into one file. An app's bundler places code by the file it comes in, not by the exports it uses, so a lazy import of the reviver would still have shipped up front.
Change
react/revive.tsnow holds the reviving code fromhydrate.ts: tagged props, slots, server trees, promises, streams, form state, and the elements an action returns. It sits behindcreateReviver(...). The runtime imports it with the first island whose props carry a tag, the first page with form state, or the first action result with elements. It starts the import beside the island's module, not after it. A failed import is retried on the next call.- Plain props (no
"$"in the text) parse and hydrate without it, as before. - Carry-over (
carryOver) shows plain props at once, as before, and tagged props at once once the reviver has loaded. Otherwise the live island keeps its place and takes its new props once the reviver and any server trees are ready. Slots are captured before the island leaves its page (react/slots.ts), and values resolve from either document. forms.tsimports the call path (./actions) on the first submit. The setters Farm calls at startup (setActionNavigator,setActionInterceptor) moved toclient/action-state.ts. The action-element reviver slot moved toclient/element-reviver.ts.- Small shared pieces got their own modules, so hydration loads without
island.tsandtree.ts:getContexts/withinClientRoot/isolateIslands(react/contexts.ts, still re-exported fromisland.ts), and the hot-update registry (react/tree-updates.ts). tsdown.config.tsgives eachsrc/{client,protocol,react}/*.tsmodule its own chunk (codeSplitting.groups, without recursive dependencies). Entry files re-export from those chunks.
No public API or option changed. Entry exports are the same.
Safety and compatibility
- Same behavior, later load. Islands with tagged props hydrate as before, after the reviver loads, which is one more async step. A test that clicked an island inside a server tree right after
actnow waits for the root (server-tree.test.tsx), as the file's other server-tree island tests already do. - Form state still reaches every
hydrateRoot: each island waits for it when the page carries it. A failed import is reported (Could not read the form state of the page.) and the island hydrates without form state, as a decode failure did before. - Carried slots:
findSlotreturns a captured island's slots from the capture alone. Before, a name that wasn't captured searched the detached island again. - Dist layout:
dist/_chunksnow has one file per browser module (65 files packed, 132.6 kB).sideEffectsis unchanged. - Unaffected: server code and the wire format.
Verification
-
New
test/revive-on-demand.test.ts. Each test loads islx in a fresh module registry and counts imports ofsrc/react/revive:- A page of plain props hydrates and responds to clicks with no import.
- Tagged props (
Date) import it once and revive. - A carry-over to tagged props imports it, keeps the island's state, and shows the new value.
- With a static import of
./reviveadded back tohydrate.ts, 2 of the 3 fail.
-
Full suite:
pnpm build,pnpm typecheck,pnpm lint,pnpm format:checkandpnpm testpassed: 247 tests in 21 files.npm pack --dry-runpassed. -
pnpm test:e2e:- First run: 43 passed, 3 skipped, and 4 failed in the development project, all from Vite's HMR socket (
WebSocket closed without opened, edits not applied). playwright test --project=developmentrerun: 18 passed, 1 skipped.
- First run: 43 passed, 3 skipped, and 4 failed in the development project, all from Vite's HMR socket (
-
Farm's islands benchmark, dash page (
benchmarks/server-components-payload/hydration.mjs):- Farm built against islx main (x27) and against this branch, with Farm's server trees moved to a lazy import (Farm PR to follow).
- Setup: 150 ms latency, 4x CPU throttling, HTTP/2, scripts at brotli 11, 21 interleaved rounds, medians, with an A/A copy of the main build.
all hydrated, 1.6 Mbit/s all hydrated, unlimited script bytes rsc778 ms 429 ms 66,887 islx main 823 ms 444 ms 78,243 this PR 791 ms 448 ms 71,323 islx main, A/A 826 ms 444 ms 78,243 islx's first-load chunks go from 13,197 B to 4,992 + 1,247 B brotli. On an unlimited link the difference is within noise.
Not verified: Farm against a published islx with this change (the numbers come from Farm builds linked to this branch's dist), browsers other than Chrome, and a quiet machine.
Summary by cubic
Pages whose islands take plain JSON props no longer load islx's reviving and server-action code with their first island: the reviver loads only when an island's props carry a tag, the page carries form state, or an action returns elements, and the action call path loads on the first form submit. On Farm's islands benchmark dash page, islx's first-load code drops from 13.2 to 6.2 KB brotli and all islands hydrate 32 ms sooner at 1.6 Mbit/s (823 → 791 ms, with an A/A copy of the main build at 826).
Behavior
- Islands with tagged props now hydrate after the reviver loads, one extra async step; everything else behaves as before.
- A carried-over island shows plain props at once and tagged props once the reviver loads; otherwise the live island keeps its place and takes its new props when ready.
- A failed chunk import retries on the next call; a failed form-state read is reported and the island hydrates without it.
- The dist is split into one chunk per browser module, so an app's bundler can leave unneeded code out of a page's first load.
- No public API or option changed.