feat(renderer): hydrate client components as islands on any renderer

Merged
KI
11d ago
+1216-77

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.islands on the renderer descriptor, and withIsland(component, reference) on its server module. farm dev and farm build fail when the descriptor declares islands and the export is missing.
  • resolveFarmIsland(props, reference) from @farm.js/core/renderer: strips client:*, picks the strategy (attribute, then the module's island export, then load), numbers ids per request, encodes props with islx's codec and sends a shared value once per response. It returns undefined and logs once when a prop can't reach the browser.
  • FarmIslandAttributes for typing client:load|idle|visible|interaction in a renderer's JSX.
  • Server graph: with experimental.isolatedClientHydration: "enabled", each export of a client module is wrapped with the renderer's withIsland. Dev and production also call the renderer's wrapClientGraph / isolateServerPageGraph, so a client component inside a hydrating root renders plain.
  • Browser: dev and production entries start islands with islx's createIslandRuntime through the renderer client module's createElement and hydrateRoot (@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/renderer now builds from renderer-entry.ts, which re-exports renderer.ts plus the islands helpers, so renderer.ts keeps no local runtime imports (#3154).
  • leaf example: farm-leaf declares islands and implements withIsland with a context; leaf gains createContext / useContext; new /islands server page with three counters (client:load, module default idle, 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 sets isolatedClientHydration: "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/islx 0.0.30 (was 0.0.25): islands/server markup parts (0.0.29) and scoped dispose(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: clean
  • vitest 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-leaf tests: 33 passed (6 new withIsland server tests)
  • node --test scripts/renderer-capability-matrix.test.js: pass
  • playwright.custom-renderer.config.ts, dev and production (installed Chrome channel): 18 passed each
  • Control: with islands: false in the leaf descriptor, both islands e2e tests fail
  • playwright.server-components.config.ts in 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/islx 0.0.25 → 0.0.30 in core; the lockfile changes only the islx entry.