feat(farm-leaf): hydrate client components as islands

Merged
KI
11d ago
+403-50

Problem

Farm 0.1.84 hydrates client components as islands on any renderer that opts in (farming-labs/farm.js#3158), and leaf doesn't opt in yet. A client component used in a server page either hydrates with the whole page or not at all.

Change

  • packages/farm-leaf/src/index.ts: capabilities.islands, and client:load, client:idle, client:visible and client:interaction typed on leaf's JSX through FarmIslandAttributes.
  • packages/farm-leaf/src/server.ts: withIsland, wrapClientGraph and isolateServerPageGraph. withIsland writes what resolveFarmIsland returns: the island element around the component, and the scripts that carry its props. Inside a tree that already hydrates as one root, it renders the component plain.
  • packages/leaf/src: createContext and useContext, which that needs, on the server and in the browser.
  • app/: experimental.isolatedClientHydration: "enabled", an Islands link in layout.leaf, and an /islands page with three counters (client:load, the module's island = "idle", client:visible) that get a Date and a Map as props.
  • No browser code in farm-leaf: Farm's islands runtime hydrates each island with farm-leaf/client's createElement and hydrateRoot.
  • Tests:
    • packages/farm-leaf/test/server.test.ts: 6 cases for withIsland, plus islands in the descriptor conformance.
    • e2e/custom-renderer.spec.ts: each strategy hydrates on its own with its props intact, and a client navigation starts and ends the page's islands.
  • README: islands in the feature list and the file table, and step 5 of "Make it yours".

Safety and compatibility

  • withIsland imports resolveFarmIsland, new in Farm 0.1.84, so the app and farm-leaf move to @farm.js/core and @farm.js/cli 0.1.84, farm-leaf's peer floor rises to >=0.1.84, and @farm.js/renderer-tests moves to 0.1.0-beta.3, which accepts capabilities.islands.
  • Pages without client components, and apps that leave isolatedClientHydration off, render as before.

Verification

With main (#2) merged in, against @farm.js/core and @farm.js/cli 0.1.84 installed from npm (pnpm install --frozen-lockfile):

  • pnpm type-check: passes in all three projects.
  • pnpm test: 56 passed.
  • pnpm test:e2e (production build) and pnpm test:e2e:dev: 19 passed each, with installed Chrome.

Documentation and release

README updated. It links Farm's Islands docs. No package release: farm-leaf is private.