perf(core): load islands mode's action client when a page can call
Problem
On a page where every island hydrates at load, islands mode loads more engine code than RSC and hydrates later. In the dashboard probe (24 client components with an effect each, /dash/p0), the islands runtime chunk was 15.4 KB at brotli 11, and about 17% of it ran at load. The rest is the server action call path: the action client, the server query runtime and the query cache. A page that never calls an action still downloaded and ran all of it before hydrating.
Root cause
client/islands-runtime.ts registers farmIslandsActionInterceptor when hydration starts. The interceptor statically imports cache-invalidation, server-query-runtime and client-cache, so they sit on the runtime's static import path even though they only handle a call's result.
Change
Item 1 and the Farm half of item 2 of RFC 0013 (#3134).
- Interceptor stub. Hydration now registers
farmIslandsLazyActionInterceptor(islands-action-call.ts).- It takes the server query making the call synchronously, as before.
- It sends the call at once, in islx's order.
- Only result handling waits:
completeFarmIslandsActioncomes frominternal/islands-action-client, imported once. A failed import is retried by the next call.
- Call-time read.
beginFarmServerQueryActionmoves toserver-query-call.ts, which imports nothing. It reads the query runtime's global state, which only exists on a page that has run a server query.server-query-runtimere-exports it, soserver-query-clientis unchanged. - Preloads. The action client loads with the page when the page can call:
- The preload manifest gains
actions: the action client's chunks outside the entry and renderer. - A document whose markup holds an action reference lists them with its other modulepreloads. A reference is an action in island or client props (
"$":"a") or a server form posting one ($F_REF_). A boot document lists them with the renderer. - A client module's own import of a server function (
createServerReferencefrominternal/islx-client) starts the import when the reference is created.
- The preload manifest gains
- Unchanged. Non-React renderers (
islx/actions-client.ts) keep the eagerfarmIslandsActionInterceptor. A page there loads that module only when it holds a reference. - Build.
internal/islands-action-clientis its own tsup entry with an internal export, so the runtime'simport()and the manifest name the same file. esbuild points a dynamic import of an entry at that entry's file. - Bench. The payload benchmark gains the probe page (
app/src/app/dash) andhydration.mjs, which measures time to all hydrated with scripts at brotli 11 for both engines.
No configuration or public API change.
Safety and compatibility
- Calls. A call before the action client arrives is sent immediately. Its result waits for the import, and its rejection is handled once the client loads, not reported as unhandled first. Results, invalidations, query cache entries, declared failures and islx's stale results go through the same code as before.
- Server queries. A page that runs a server query has imported the query runtime with the query's module. The call-time read sees the query making the call, as before (unit test below).
- Costs.
- A page holding no reference in its first document pays one round trip on its first call. That covers a reference arriving later in a streamed chunk, a navigation or an action result.
- Streamed shells pass no markup to the preload scan, as for island modules today.
- Targets. The change is in the client bundle and the generated document code, the same for every deploy target.
- Unaffected. The
rscengine and isolated hydration without islands mode never load this code.
Verification
pnpm exec vitest run src/__tests__/islands-action-client.test.ts src/__tests__/client-module-preloads.test.ts src/__tests__/client-entry-server-imports.test.ts: 3 files, 40 passed- The interceptor cases now run against both interceptors.
- New case: the server query making a call is taken when the call is made. It fails when the read moves after the import (checked by making that change).
- Server query, islands action and form action suites (11 files): 133 passed
src/__tests__/server-actions-any-renderer.test.ts: 6 passedpnpm --filter @farm.js/core type-check: clean.oxfmtandoxlinton the changed files: clean.pnpm --filter @farm.js/core test: 714 files and 6636 tests passed.- 51 files (18 tests) failed because this worktree had not built the sibling packages they load:
@farm.js/mcp,otel,auth,auth0,workos,stripe,polar,autumn,jobs,resend,ai,vue,svelteandrenderer-tests("Failed to resolve entry for package"). None of them cover islands mode or actions. CI builds them.
- 51 files (18 tests) failed because this worktree had not built the sibling packages they load:
FARM_SERVER_COMPONENTS_MODE=islands pnpm exec playwright test -c playwright.server-components.config.ts(production build, Chrome), in three runs:- The first full run stopped during test 33 with no failure and no error message, while the disk had 1.5 GB free. Tests 1 to 32 (
battle.spec.ts,client-plugins.spec.ts, part ofcompat.spec.ts) reported no failure. compat.spec.tson its own: 22 passed.contract,next-parity,search-params,wireandcspspecs: 103 passed, 5 skipped.- Includes a new case:
/action-propsand/actionlist the action client chunk,/timingdoes not.
- The first full run stopped during test 33 with no failure and no error message, while the disk had 1.5 GB free. Tests 1 to 32 (
- The same suite with
FARM_SERVER_COMPONENTS_MODE=rsc: 121 passed, 35 skipped (islands-only cases). - Islands mode against
farm dev(FARM_SERVER_COMPONENTS_SERVER=dev,-g "action|query|form"): 36 passed, 2 skipped (production-only). - Islands mode with the nonce CSP (
FARM_SERVER_COMPONENTS_CSP=nonce FARM_SERVER_COMPONENTS_CSP_ALL=1,-g "action|query|form|nonce"): 38 passed, so the lazyimport()runs under a nonce policy.- The new preload case fails in that mode. A nonce policy keeps documents free of modulepreload links, so there is nothing to list.
- The existing "leaves out the renderer's chunks" case fails there the same way (checked).
CSP_ALLis not in CI.
Benchmark. node benchmarks/server-components-payload/hydration.mjs --modes rsc,islands-before,islands-after --down 1600 (new in this PR).
- Setup:
/dash/p0, a cold visit over HTTP/2 with every script at brotli 11 for both engines. 150 ms latency, 4x CPU throttling, Chrome, macOS, load average 13 to 16. - Builds: interleaved, 11 rounds. The "before" build is
mainat e9da65cb9 (the base differs only in preview packages).
| Network | rsc | islands, before | islands |
|---|---|---|---|
| 1.6 Mbit/s | 801 | 876 | 860 |
| 1.6 Mbit/s, second run | 891 | 864 | |
| Unlimited | 457 | 540 | 529 |
- Control: in the second run, an A/A copy of the before build had the same median (891 ms).
- Unlimited: the 11 ms is within that run's spread (410 to 672 ms).
- Bytes: scripts on the wire went from 82.3 KB to 80.0 KB (
rsc: 66.8 KB). The islands runtime chunk went from 51.3 KB to 42.1 KB raw, 15.4 to 13.1 KB at brotli 11. The action client chunk is 9.7 KB (2.8 KB br11), and no document of the benchmark app loads it. - Other sections:
node benchmarks/server-components-payload/measure.mjs --skip-build --modes islands-before,islands-aftergives identical results for docs, shop, blog and guide. Document bytes, first-load JS (27.8 KB), navigation data and page errors (0) are unchanged. Those pages load the islands runtime only on an interaction. - Gap:
islandsstill hydrates this page 59 ms afterrsc. The remaining RFC 0013 items and islx's runtime account for that. The first paint (about 400 ms against 250) also waits on the empty stylesheet link that #3136 removes.
Not verified:
- the full Windows CI matrix; this ran on macOS only.
- deploy targets other than
node-server, though the change is in the client bundle and generated document code shared by all targets. - a slow import racing a rejected call in a real browser. It is covered only by the unit test's ordering, where the import resolves after the response.
Documentation and release
- No user-facing docs change: no configuration, API or behavior a user observes changes, only when the code loads.
- RFC 0013 (#3134) gets the measured result.
benchmarks/server-components-payloadREADME: new section.