perf(react): load the reviver and action client only when a page uses them

Merged
KI
12d ago
+984-737

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.ts imported the reviver, createSlotTree (tree.ts), createServerReference (actions.ts), the value store and the codec statically. forms.ts imported createServerReference statically, and island.ts (where the React contexts lived) imports tree.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.ts now holds the reviving code from hydrate.ts: tagged props, slots, server trees, promises, streams, form state, and the elements an action returns. It sits behind createReviver(...). 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.ts imports the call path (./actions) on the first submit. The setters Farm calls at startup (setActionNavigator, setActionInterceptor) moved to client/action-state.ts. The action-element reviver slot moved to client/element-reviver.ts.
  • Small shared pieces got their own modules, so hydration loads without island.ts and tree.ts: getContexts/withinClientRoot/isolateIslands (react/contexts.ts, still re-exported from island.ts), and the hot-update registry (react/tree-updates.ts).
  • tsdown.config.ts gives each src/{client,protocol,react}/*.ts module 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 act now 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: findSlot returns a captured island's slots from the capture alone. Before, a name that wasn't captured searched the detached island again.
  • Dist layout: dist/_chunks now has one file per browser module (65 files packed, 132.6 kB). sideEffects is 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 of src/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 ./revive added back to hydrate.ts, 2 of the 3 fail.
  • Full suite: pnpm build, pnpm typecheck, pnpm lint, pnpm format:check and pnpm test passed: 247 tests in 21 files. npm pack --dry-run passed.

  • 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=development rerun: 18 passed, 1 skipped.
  • 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/sall hydrated, unlimitedscript bytes
    rsc778 ms429 ms66,887
    islx main823 ms444 ms78,243
    this PR791 ms448 ms71,323
    islx main, A/A826 ms444 ms78,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.