perf(core): load islands server trees only on pages that have one
Problem
On the dash page of the islands benchmark, where all 24 components hydrate, islands mode was 40 ms behind rsc at 1.6 Mbit/s. Most of the difference was islx's React runtime: the islands runtime chunk (13.1 KB brotli) carried the code that revives slots, server trees, actions and tagged values, though the page's islands take plain props.
Root cause
- islx 0.0.25 shipped that code in one chunk. islx #89 (in 0.0.28) splits it by module and imports the reviving code with the first island, form state or action result that needs it.
- Farm's islands runtime imported islx's server-tree helpers (
createServerTreeElement,createServerTreeLayout) statically. They reach the reviving code, so with 0.0.28 every page would still load it with the renderer.
Change
- Core uses islx's split (0.0.28, #89 and #91;
@farm.js/islx/reactand the wire format are unchanged).mainhas since moved to islx 0.0.30 (#3158), and this branch takes that pin and lockfile from the merge, so the PR no longer changes the dependency. - Server trees load with the first one.
createFarmServerTreeElementandcreateFarmServerTreeLayoutimport islx's helpers from a new internal entry,@farm.js/core/internal/islands-server-tree. After a failed import, the next call imports them again. Both already returned promises that their callers await. - A document that holds a server tree preloads them. The preload manifest gains
serverTrees, that entry's chunks outside the entry's and the renderer's. A document whose markup holds a<script type="f/tree">lists them with the renderer, and a navigation's response with its modules. This is how #3143 handles the action client.
No option or public API changed.
Safety and compatibility
- Pages with server trees keep their timing. Their documents preload the same code at the same moment, now in more chunks. Shop and guide in the benchmark are such pages; see first navigation below.
- Dev: the generated dev entry imports the same runtime; the islands dev suite passes.
- CSP nonce mode keeps the documents it had (no modulepreload links), as before; its suite passes.
- Other modes and renderers: only islands mode with React uses these modules.
Verification
- New tests:
islands-server-tree-loading.test.ts: the runtime doesn't import the server trees until the first one, imports them once, and again after a failed import. Both tests fail againstmain's runtime.client-module-preloads.test.ts: the manifest lists the server trees' chunks outside the renderer's. A document that holds a tree lists them with the renderer (boot) or its modules (navigation); a page of islands only never does.
pnpm --filter @farm.js/core type-check: clean. 12 focused islands and preload test files: 213 passed.pnpm --filter @farm.js/core test: 6,709 passed. 18 failed in 49 files, all of them because packages without a localdistfail to resolve (integration-utils,stripe,jobs,mcp,otel,auth,auth0,autumn,polar).dev-module.test.tsfailed twice under load and passes alone.FARM_SERVER_COMPONENTS_MODE=islands playwright test --config playwright.server-components.config.ts:- Production: 148 passed, 8 skipped.
- Client plugins: 3 passed.
- Vercel target: 148 passed, 8 skipped.
- Dev server: 140 passed, 16 skipped.
- CSP nonce: 4 passed.
- Measured and tested against the islx 0.0.28 tarball (shasum
5574e985), the one now on npm. - After merging
main(islx 0.0.30):type-checkclean, core builds, and 33 islands, preload and renderer test files pass (281 tests). - After rebasing onto #3144:
type-checkclean, and 74 islands, build and preload test files pass (796 tests). The Playwright runs above are from before the rebase; #3144 changes docs adapters only.
hydration.mjs, dash page. 150 ms latency, 4x CPU, HTTP/2, scripts at brotli 11, 21 interleaved rounds, medians in ms:
rsc | main | this PR | main, A/A | |
|---|---|---|---|---|
| 1.6 Mbit/s, all hydrated | 776 | 816 | 789 | 817 |
| Unlimited, all hydrated | 414 | 422 | 423 | 424 |
| Script bytes | 66,887 | 76,572 | 69,636 | 76,572 |
- The gap to
rscat 1.6 Mbit/s goes from 40 ms to 13 ms (islx 0.0.25 onmain). An earlier build of this branch measured 782 against 815 and 812. - Unlimited, the difference is within the A/A pair's spread.
- The islands runtime chunk goes from 13,138 to 4,902 bytes, plus islx's element reviver (1,177).
After merging main (islx 0.0.30). The same probe against current main and an A/A copy, medians in ms:
rsc | main | this PR | main, A/A | |
|---|---|---|---|---|
| 1.6 Mbit/s, all hydrated, 41 rounds | 778 | 814 | 783 | 813 |
| Unlimited, all hydrated, 21 rounds | 418 | 428 | 429 | 427 |
| Script bytes | 66,887 | 76,464 | 69,637 | 76,464 |
- This PR was faster than
mainin all 41 rounds at 1.6 Mbit/s (paired median 31 ms, bootstrap 95% interval 27 to 35 ms). The gap torscgoes from 36 ms to 5 ms. mainwith islx 0.0.30 still sends 76.5 KB of script: the split alone changes nothing while the islands runtime imports the server trees up front.measure.mjson both builds: first-load script is 26.6 KB on each of the other four sections; documents and navigation data are within 0.2 KB raw.- The
rscbuild and the tables below are from before the merge.
The other four sections.
measure.mjs | main | this PR |
|---|---|---|
| First-load script, each section | 26.5 KB | 26.6 KB |
| Document, shop (raw) | 31.0 KB | 31.2 KB |
| Document, guide (raw) | 129.2 KB | 129.4 KB |
| Navigation data, shop | 4.4 KB | 4.5 KB |
| Navigation data, guide | 9.6 KB | 9.7 KB |
- First-load script: the entry names one more chunk in its preload map (33 bytes), and Rolldown's runtime gains the namespace helper islx 0.0.28's actions chunk uses (55 bytes).
- Shop and guide documents list the server trees' chunks.
- The renderer's islx code: docs and blog hold no server tree and go from 13.1 KB to 6.1 KB. Shop and guide preload the trees too, 14.0 KB in all, 842 bytes more than the one chunk before, from islx's split.
first-navigation.mjs --latency 150 --down 1600 --rounds 11, one section per run, medians in ms:
| Section | Input | main | this PR | main, A/A |
|---|---|---|---|---|
| docs | hover | 80 | 80 | 80 |
| shop | hover | 101 | 99 | 100 |
| blog | hover | 155 | 158 | 157 |
| guide | hover | 153 | 158 | 157 |
| docs | tap | 212 | 213 | 213 |
| shop | tap | 234 | 232 | 232 |
| blog | tap | 274 | 275 | 273 |
| guide | tap | 284 | 290 | 292 |
Blog and guide after a hover, and blog after a tap, are 1 ms over both main and its A/A copy. The A/A pair differs by up to 4 ms here (guide hover 153 against 157).
Not verified:
- The cloudflare target.
- The whole contract suite under CSP nonces (
FARM_SERVER_COMPONENTS_CSP_ALL=1, not a CI variant): 5 tests fail. Three fail on next-themes' inline script. Two check modulepreload lists, which nonce mode leaves out by design. None of the five involves server trees, but I didn't run that variant onmainto compare. - Windows; the change is in generated browser and server JavaScript and a new package entry.
Documentation and release
- RFC 0013 gains decision item 5 and its results.
@farm.js/coregains the internal export./internal/islands-server-tree(for the app's bundler, like./internal/islands-action-client).- islx 0.0.28 is released: farming-labs/islx#90, on npm, and GitHub release
v0.0.28.
Summary by cubic
Loads islx's server-tree helpers only on pages that actually have one, so islands-only pages no longer download the code that revives server trees, actions, and tagged values. islx 0.0.28 splits its React runtime into per-feature modules, and main now pins islx 0.0.30; pages with a server tree keep their loading behavior via preloads. On the islands benchmark's dash page, the gap to the RSC baseline at 1.6 Mbit/s drops from 40 ms to 13 ms, and script bytes go from 76.6 KB to 69.6 KB; after merging main (islx 0.0.30), the gap to rsc is 5 ms over 41 rounds.
Key changes
- The islands runtime imports the server-tree helpers from a new internal entry on the first server tree a page has, and retries after a failed import.
- The preload manifest lists the helpers' chunks, and documents whose markup holds a server tree preload them with the renderer, like the action client.
- No options or public APIs change.