feat(renderer): hydrate client components as islands on any renderer
Merged
KI
11d ago+1216+1216+0123456789012345678901234567890123456789-77-77-01234567890123456789
Problem
Isolated client hydration only works with React. On any other renderer, a "use client" component rendered from a server page never hydrates, so a custom renderer can't ship islands without Farm adding code for it.
Change
Implements RFC 0012 (#3132), steps 1, 2 and 4.
capabilities.islandson the renderer descriptor, andwithIsland(component, reference)on its server module.farm devandfarm buildfail when the descriptor declaresislandsand the export is missing.resolveFarmIsland(props, reference)from@farm.js/core/renderer: stripsclient:*, picks the strategy (attribute, then the module'sislandexport, thenload), numbers ids per request, encodes props with islx's codec and sends a shared value once per response. It returnsundefinedand logs once when a prop can't reach the browser.FarmIslandAttributesfor typingclient:load|idle|visible|interactionin a renderer's JSX.- Server graph: with
experimental.isolatedClientHydration: "enabled", each export of a client module is wrapped with the renderer'swithIsland. Dev and production also call the renderer'swrapClientGraph/isolateServerPageGraph, so a client component inside a hydrating root renders plain. - Browser: dev and production entries start islands with islx's
createIslandRuntimethrough the renderer client module'screateElementandhydrateRoot(@farm.js/core/internal/renderer-islands). Under a hydrating layout, Farm starts the page's islands after each render and disposes the ones whose markup was replaced. @farm.js/core/renderernow builds fromrenderer-entry.ts, which re-exportsrenderer.tsplus the islands helpers, sorenderer.tskeeps no local runtime imports (#3154).- leaf example:
farm-leafdeclaresislandsand implementswithIslandwith a context; leaf gainscreateContext/useContext; new/islandsserver page with three counters (client:load, module defaultidle,client:visible).
Differences from the RFC:
- The runtime adds
prune(): a hydrating layout's root replaces page markup without telling the runtime. - Integration providers don't wrap renderer islands: the server can't render them around an island synchronously. This is documented under Limits.
Safety and compatibility
- Opt-in twice: the renderer declares
capabilities.islands, and the app setsisolatedClientHydration: "enabled". - React, islands mode (
serverComponents: { mode: "islands" }) and renderers without the capability take the same code paths as before. The official renderers are unchanged. - An island module edit reloads the page in dev, with no HMR for it.
- Core now depends on
@farm.js/islx0.0.30 (was 0.0.25):islands/servermarkup parts (0.0.29) and scopeddispose(scope)(0.0.30, farming-labs/islx#94). It also brings 0.0.26 to 0.0.28 to React islands mode, verified below. #3155 pins 0.0.28 and needs this pin kept when it merges.
Verification
On the published @farm.js/islx 0.0.30:
pnpm --filter @farm.js/core type-check: cleanvitest run src/__tests__/renderer-islands.test.ts: 11 passed (resolveFarmIsland,assertFarmRendererServerModule,usesFarmRendererIslands, transform)- Core renderer, islands, isolated, universal-build and dev-renderer suites: 46 files, 519 passed
farm-leaftests: 33 passed (6 newwithIslandserver tests)node --test scripts/renderer-capability-matrix.test.js: passplaywright.custom-renderer.config.ts, dev and production (installed Chrome channel): 18 passed each- Control: with
islands: falsein the leaf descriptor, both islands e2e tests fail playwright.server-components.config.tsin islands mode (React islands on the new islx): 148 passed in production, 140 in dev, none failed- Full core suite: the failures were all environmental (workspace packages not built in this worktree, plus load timeouts). The 5 non-package failures pass when run alone (138 tests).
Not verified: Windows, CI, other deploy targets than Node.
Documentation and release
docs/renderers/custom: new Islands section, descriptor and server-module references, Limits.docs/configuration: isolated hydration on other renderers.- examples/custom-renderer README.
@farm.js/islx0.0.25 → 0.0.30 in core; the lockfile changes only the islx entry.