feat(docs): run docs adapters on any renderer, with its renderer passed in

Merged
KI
12d ago
+429-78

Problem

  • A docs adapter ran only in React apps. Every adapter gate checked isReactRenderer(), and the descriptor had only a react slot for the UI module.
  • So a Svelte, Vue, Solid, Preact, or custom-renderer app could not use @farming-labs/farmjs, even with a UI written for its renderer.
  • Dev and production disagreed for that pairing: production served Farm's built-in docs renderer, while dev served the adapter's pages and never hydrated them.
  • A docs UI meant to run on any renderer had no way to get the app's renderer from Farm.

Root cause

  • The React gate came in with the Solid renderer (#323) as a precaution. No adapter contract needed it.
  • The adapter's server already renders its own UI module, loaded through Farm's module loader, so nothing in the protocol is React-specific except the field names.

Change

  • The descriptor gains ui, the UI module, and renderer, the Farm renderer that UI is written for (default "react"). react stays as a deprecated alias.
  • Farm passes the adapter loadUIModule(), and still passes loadReactModule() to a React UI.
  • New loadRenderer() for the adapter's handler: renderer.server through the app's module loader in dev, and in production the module the server entry already imports ({ name, createElement, Fragment, renderToString } on React). A renderer's generateHydrationScript(), such as Solid's, comes with it. It loads only when the UI asks.
  • The client entry imports ui and calls its hydrateFarmDocs(). For a renderer other than React it also imports renderer.client and passes it as renderer. React UIs load nothing new.
  • The React-only gates are gone from the dev server, the production build, routing, prerendering, and analytics. All of them now read hasFarmDocsRuntimeAdapter().
  • Config resolution stops when an adapter's renderer is not the app's, with the fix in the message.
  • Auto-detection accepts a descriptor that names ui. It still runs only for React apps.

Safety and compatibility

  • Protocol stays 1. Descriptors with only react behave as before in React apps.
  • Behavior change: an app with another renderer whose @farming-labs/farmjs has no UI for it now stops at startup, and the error names both fixes. Before, production served the built-in renderer and dev served unhydrated adapter pages. With farming-labs/docs#728, withDocs() alone builds those pages with the app's renderer.
  • Older adapters ignore loadUIModule and loadRenderer, and read loadReactModule, which React UIs still get.
  • Edge targets: a UI other than React has no edge compiler, so an edge build stops as before.

Verification

  • pnpm exec vitest run src/__tests__/docs-adapter.test.ts src/__tests__/docs-adapter-client.test.ts src/__tests__/docs-framework-detect.test.ts: 35 passed. The new and changed tests fail on the previous adapter.ts, adapter-client.ts, config.ts, framework-detect.ts, and universal-build.ts.
  • tsc --noEmit -p . in packages/farm: clean. oxlint and oxfmt --check on the changed files: clean.
  • End to end with packed tarballs of this branch and docs#728, in farming-labs/leaf (a custom renderer) with withDocs(defineConfig({ renderer: leaf() })):
    • leaf's e2e suite: 17/17 against farm dev and 17/17 against the production build (node-server, installed Chrome). That includes 4 new docs tests: leaf renders the docs on the server, hydration keeps the server's DOM, the theme toggle persists, page links work, and the mobile sidebar opens and closes.
    • The production client JS contains no React. The docs runtime is one chunk, 4.2 KB over the wire.
  • The docs conformance suite from docs#728 passes on React, Preact, Vue, Svelte, Solid, and leaf.
  • Copies of the Preact, Solid, Vue, and Svelte examples with withDocs() and leaf's docs pages, same tarballs, installed Chrome: leaf's 4 docs tests pass against farm dev and the production build for each, 32/32. Preact dev needed a fix in docs#728 (aa84f372): Preact's react alias reached the docs Markdown render in dev.
  • pnpm docs:build: passes (the cron sync skips outside a Vercel production build). pnpm docs:check-navigation: 115 pages.
  • Serverless: the Preact copy built for deploy: { target: "vercel" }, its function's fetch handler served locally with static/: leaf's 4 docs tests pass.

Not verified:

  • A real Vercel deployment, and the other serverless targets.

Documentation and release

  • docs-engine: "Docs adapters on any renderer" covers ui, the renderer handoff, the default UI from docs#728, its limits, and the conformance suite.
  • The Svelte, Vue, Solid, Preact, and custom renderer pages say withDocs() builds docs pages with that renderer.
  • Companion: farming-labs/docs#728. Both need a release before leaf's docs branch can land.