feat(islands): give renderers that build elements the island's parts

Merged
KI
12d ago
+198-19

Problem

renderIslandMarkup (#91) returns the island as one finished HTML string, so the renderer has to render the component to a string first. A renderer that builds elements (Vue's h, a JSX runtime), or renders the island inside its page's own render, needs to put <f-island> around the component in its own tree. For that it needs the tag, the attributes and the scripts as values. Farm's islands interface for renderers (farming-labs/farm.js#3132) builds on this.

Change

  • resolveIslandMarkup(input) in @farm.js/islx/islands/server takes renderIslandMarkup's input without html and returns the parts:
    • values: the value scripts to write first, for values this response hasn't sent yet;
    • element: { tag: "f-island", attributes }. Attribute values are unescaped; the renderer escapes them as it writes any attribute;
    • script: the props script's attributes, and its text already escaped, to write raw.
  • renderIslandMarkup now joins those parts around the HTML.
  • Same props rules: a function or promise prop throws and names the island, and one sent set per response sends a shared value once.

Safety and compatibility

  • Adds one function and four types (ResolveIslandMarkupInput, IslandMarkupParts, IslandScript, IslandAttributes). IslandMarkupInput keeps its fields and now extends ResolveIslandMarkupInput.
  • renderIslandMarkup's output is unchanged. Its exact-output test passes as is. The only newly escaped values are strategy and the script types, which have nothing to escape.
  • No change to the client runtime, @farm.js/islx/react, the wire format, dependencies or the lockfile.

Verification

On this branch, rebased onto 0.0.28:

  • pnpm build, pnpm typecheck, pnpm lint, pnpm format:check, pnpm test: 262 passed in 22 files, 4 of them new:
    • the parts match what renderIslandMarkup writes, with attributes unescaped and </script> kept out of the script text;
    • a value shared by two islands is sent once;
    • a function prop throws and names the island;
    • an island built as DOM elements from the parts (jsdom) hydrates with the runtime, keeps the server's button, and decodes its Date and Map props.
  • Breaking the element's props attribute in resolveIslandMarkup fails 8 tests.
  • pnpm test:e2e (installed Chrome): 73 passed, 3 skipped. The skips are the existing dev-only and production-only tests.

Not verified:

  • No adapter in examples/renderers builds islands from the parts yet; only the jsdom test does.
  • Farm doesn't use it yet.
  • Firefox, Safari, Windows.

Documentation and release

  • README: a paragraph and example under "Islands for any renderer".
  • The CHANGELOG is written in the release commit.

Summary by cubic

Adds resolveIslandMarkup to @farm.js/islx/islands/server so renderers that build elements instead of strings can get an island's wire format as parts (value scripts, the <f-island> element, and the props script) rather than one finished HTML string. renderIslandMarkup now joins those parts, and its output is unchanged.

  • Attribute values come unescaped for the renderer to escape; props script text is already escaped and written raw.
  • Function and promise props throw and name the island; a per-response sent set sends shared values once.
  • Adds one function and four types, with no change to the client runtime, @farm.js/islx/react, the wire format, dependencies, or the lockfile.