feat(renderer): let client components in a renderer's own format be islands

Merged
KI
11d ago
+368-27

Problem

A client component written in a renderer's own file format never becomes an island. With experimental.isolatedClientHydration: "enabled" and a renderer that declares capabilities.islands (#3158), a .leaf component with "use client", rendered from a server page, comes out as plain HTML with no island and no client code. The same component as .tsx hydrates.

Root cause

Farm plans which client components become islands by reading source files before any compiler runs (utils/client-component.ts). A .leaf file isn't JavaScript, so the plan finds no "use client" in it and never selects it, and the server graph never wraps it with withIsland. The renderer's Vite plugin runs before Farm's transform, so the build would see the directive, but only for modules the plan selected. The custom-renderer example's islands page is .tsx, so no test covered a .leaf client component.

Change

  • renderer.ts: an optional compile(source, file) on the descriptor, which returns the JavaScript or TSX module the file compiles to. resolveFarmRenderer() rejects a value that isn't a method, with the other descriptor issues.
  • utils/renderer-hydration.ts: the scope the route scan records per app also keeps compile. compileFarmRendererSource() runs it for a file with one of the renderer's componentExtensions, and names the file when it throws or returns something other than a string.
  • utils/client-component.ts: readIfExists(), which every read in the plan goes through, reads such a file as the module compile() returns, memoized by path and source. A quick extension check keeps apps without compile() on the old path. hydration.resolve() still gets the file as written.
  • Example: farm-leaf passes its .leaf compiler as compile(). The compiler now keeps a module script's "use client" ahead of the imports it hoists, or the directive isn't the module's first statement. /islands/leaf renders two planter.leaf islands with different strategies.

Safety and compatibility

  • Opt-in. A renderer without compile() reads files as before, and React apps take the same path (mayCompileFarmRendererSource returns false when no renderer scope is recorded).
  • compile() only runs in Node while Farm scans the app, for the renderer's own extensions. It never reaches a bundle, and the Vite plugin still compiles the file for the build.
  • No change to built-in renderers.

Verification

  • pnpm exec vitest run src/__tests__/renderer-routes.test.ts src/__tests__/client-component.test.ts: 127 passed. Without the client-component.ts change, "plans an island for a client component in the renderer's own format" and "names the file when compile() throws or returns no source" fail.
  • pnpm --filter @farm.js/core type-check: passes. node --test scripts/renderer-capability-matrix.test.js: 1 passed.
  • playwright test -c playwright.custom-renderer.config.ts (installed Chrome) against the production build and farm dev: 19 passed each. Without the example's compile(), the new /islands/leaf test fails: no <f-island> for planter.leaf.
  • pnpm --filter farm-leaf test in the example: 51 passed, including the new directive test.
  • farming-labs/leaf with these changes, against @farm.js/core packed from this branch: type-check, 57 unit tests, and e2e 20/20 in production and dev.
  • Not run: the full core suite in a clean environment. Locally, 20 tests fail because this fresh worktree has no built dist/ for workspace packages (@farm.js/integration-utils, @farm.js/stripe, @farm.js/mcp, @farm.js/otel, farm-auth); none touch the islands plan.

Documentation and release

  • docs/renderers/custom: compile() in the descriptor list, "Your own file format", "Hydration", and "Islands".
  • examples/custom-renderer/README.md, and the example's route types for /islands/leaf.
  • Ships in the next shared release. leaf moves to it in a follow-up PR there.

Summary by cubic

Client components in a renderer's own file format (.leaf, for example) now become islands instead of rendering as plain HTML. Farm plans which components hydrate by reading source files before any compiler runs, so it never found "use client" in a non-JS file.

  • Renderers can now provide compile(source, file) on the descriptor; Farm reads a file with one of the renderer's componentExtensions as the module compile() returns.
  • hydration.resolve() still receives the file as written, before compile().
  • Opt-in: renderers without compile() read files as before, and built-in renderers are unchanged.
  • The custom-renderer example passes its .leaf compiler and adds /islands/leaf with two planter.leaf islands.