feat(islands): hydrate islands with any renderer's hydrate function

Merged
KI
12d ago
+1815-13

Problem

islx's wire format (<f-island>, the props script, values sent once, action props, strategies, held clicks) never depended on React, but @farm.js/islx/react is the only runtime that hydrates it. Preact, Vue, Svelte and Solid apps can't use islx islands, and Farm turns islands off for every renderer but React.

Change

  • @farm.js/islx/islands/server: renderIslandMarkup({ src, export, strategy, id, props, html, sent }) wraps the HTML from any renderer's string renderer in the island element and its props script.

    • Props go through the codec: Date, Map and the other tagged types, server actions, and values sent once per response.
    • A function, promise or element prop throws and names the island.
  • @farm.js/islx/islands/client: createIslandRuntime({ renderer, load, schedule }) finds the islands, waits for each one's strategy, reads its props, and calls the renderer's one function:

    defineIslandRenderer({
      hydrate(container, Component, props, island) {
        const root = hydrateRoot(container, createElement(Component, props));
        return { unmount: () => root.unmount() };
      },
    });
    

    island is { id, src, export, strategy }. Solid uses id as its renderId, so its page-wide hydration keys don't collide between islands.

  • Failure: a hydrate that throws gets the island's server HTML back and is reported. The island isn't marked hydrated, and held clicks aren't replayed onto it.

  • examples/renderers: adapters for Preact 11.2, Vue 3.5, Svelte 5.59 and Solid 1.9, about ten lines each, with a load, an interaction and a visible island on each renderer's page.

Not in this runtime yet: slots, promise props, server trees, useActionState form state. They stay in @farm.js/islx/react.

Safety and compatibility

  • New entries only. @farm.js/islx/react and the wire format are unchanged.
  • Size: the neutral client is 6.0 KB gzip up front (rolldown minify). The action client (2.7 KB) loads only for an island whose props carry an action.
  • Solid 1.9 stops hydrating after the page's first delegated event, so an island that wakes later gets fresh DOM from Solid. It still works. The README says so, and an e2e test pins it.
  • Lockfile: it adds the example's packages and nothing else.
    • tsdown asks for rolldown latest, so any install that rewrites the lockfile resolves 1.3.3, which changes islx's dist (chunk hashes, import order).
    • The lockfile keeps 1.2.16. The dist is byte-identical to this branch's build before the lockfile change.
  • Dev server: the example's dev server attaches Vite's HMR socket to its own HTTP server. Middleware mode otherwise takes port 24678, which the React example's dev server holds in the same e2e run.

Verification

  • pnpm build, pnpm typecheck, pnpm lint, pnpm format:check, pnpm test: 258 passed in 22 files, 11 of them new in test/islands.test.ts.
  • npm pack --dry-run: 70 files, including dist/islands/{client,server}.
  • pnpm test:e2e (installed Chrome): 73 passed, 3 skipped. The skips are existing dev-only and production-only tests.
  • New e2e/renderers.spec.ts: 26 tests, against the production build and the dev server, with no console errors or warnings in any of them.
    • Load island: keeps the server's nodes, decodes its Date and Map props, and counts clicks.
    • Interaction island: the click that wakes it is replayed once; the next click counts 12, not 13.
    • Visible island: waits for the scroll and keeps the server's nodes.
    • Solid: an island that wakes after the first click works, on fresh DOM.
  • The tests catch broken adapters. A Vue server/client prop mismatch fails them in production and dev, and so does a Preact root that replaces the DOM.
  • tsc -p examples/renderers passes. oxfmt --check and oxlint pass on the 17 new files.

Not verified:

  • Solid 2.0: its Vite plugin is still prerelease.
  • Firefox and Safari.
  • e2e on Windows: CI runs it on Linux only.
  • Farm doesn't use these entries yet.

Documentation and release

  • README: "Islands for any renderer" section, including Solid's bootstrap and renderId, PREHYDRATION_SCRIPT, and the example.
  • SPEC: the layers table lists the new entries. The CHANGELOG is written in the release commit.

Summary by cubic

Adds renderer-neutral islands so Preact, Vue, Svelte, and Solid apps can hydrate islx islands, not just React. Previously only @farm.js/islx/react could hydrate the wire format; now any renderer provides a single hydrate function and two new experimental entries handle the rest. The React runtime, wire format, and codec are unchanged.

How it works

  • renderIslandMarkup wraps HTML from any renderer's string renderer in the island element and props script, with props through the codec; function or promise props throw naming the island.
  • createIslandRuntime waits for each island's strategy, decodes its props, and calls hydrate(container, Component, props, island); a throwing hydrate restores the server HTML and skips replaying held clicks.
  • examples/renderers adds about-ten-line adapters for Preact 11.2, Vue 3.5, Svelte 5.59, and Solid 1.9, with e2e coverage in production and dev.

Compatibility notes

  • New entries only; slots, promise props, server trees, and useActionState form state stay in @farm.js/islx/react.
  • Solid 1.9 stops hydrating after the page's first delegated event, so later islands get fresh DOM; documented and pinned by an e2e test.
  • The lockfile pins rolldown 1.2.16 because 1.3.3 changes the published dist; any install that rewrites the lockfile changes @farm.js/islx's dist.