feat(farmjs): render docs on any Farm renderer without a custom UI

Merged
KI
12d ago
+2098-41

Problem

  • @farming-labs/farmjs renders docs pages only with its React UI, so a Farm app with another renderer (Svelte, Vue, Solid, Preact, or a custom one like leaf) cannot use it.
  • Most of the adapter is already renderer-neutral: content, navigation, search, Markdown, llms.txt, MCP, and the HTML document. React is used in one place, where renderPage() renders FarmDocsPage.

Change

A UI for any renderer:

  • withDocs({ ui: { module, renderer } }) names a docs UI module and the Farm renderer it is written for. The module exports renderFarmDocsPage() and hydrateFarmDocs(), typed by FarmDocsUIModule.
  • The server loads the UI through Farm's loadUIModule(), falling back to loadReactModule(). A UI that exports FarmDocsPage still renders through React, which loads only then.
  • The descriptor carries ui and renderer. The React UI keeps react for Farm releases that read only that field; another UI drops react and edgeCompiler.
  • New @farming-labs/farmjs/navigation: createFarmDocsNavigator(), for any UI.

No UI to write:

  • On a renderer other than React, withDocs() alone uses the new @farming-labs/farmjs/universal UI. It builds each page from host elements with the renderer Farm passes in (farm.js#3144): createElement() and renderToString() on the server, createElement() and hydrateRoot() in the browser.
  • It keeps the React UI's DOM and classes, so the theme stylesheet styles it. The few rules the React layout sets as props are appended to /__farm_docs/browser.css, scoped to [data-farm-docs-ui]. An inline <style> broke Svelte's hydration, which writes a marker inside raw text.
  • Markdown compiles with the existing MDX pipeline and theme components and is written to HTML on the server only. A browser export condition gives browsers an entry without it, so neither Vite's dev optimizer nor the client build bundles React or the compiled pages.
  • Plain DOM listeners run folders, the theme toggle, the mobile sidebar, and copy buttons. unmount() removes them.
  • The document head gets the renderer's generateHydrationScript(), which Solid hydrates against.
  • New @farming-labs/farmjs/testing: defineFarmDocsRendererConformance({ name, server, client, hydrationAttributes }) checks that a renderer writes the page as a reference renderer does and hydrates it without replacing the DOM.
  • withDocs() reports every problem with ui at once, and says why shell has nothing to wrap on another renderer.
  • In development, an app that aliases react (Preact's preset points it at preact/compat) no longer splits the Markdown render between two libraries. A resolveId hook ordered before Vite's alias plugin loads real React for this package, the theme, and the pages, as Farm's production server already does. Without it, Preact's string renderer dropped the headings and code blocks fumadocs-ui writes with React.
  • A table of contents: the page's h2 to h4 headings with an id, in the React UI's #nd-toc column and classes. The server reads them from the Markdown's HTML; the browser reads the list back from the markup. An IntersectionObserver marks the headings on screen with data-active. It follows theme.ui.layout.toc.enabled and shows on wide screens only.
  • On Solid, #farm-docs-root gets data-farm-solid-render-id="". Without it, farm-solid guesses the render id by cutting "00" off the first hydration key, and a root key such as a600 (the 61st element) reads as render id a6, so hydration fails. The table of contents made the page exactly that long.

Safety and compatibility

  • React apps render the same way. browser.css gains about 1.3 KB of rules their pages never match.
  • The universal UI needs farm.js#3144. Without it, the UI throws and names the upgrade.
  • Limits: no search box or client navigation between docs pages yet, and no table of contents popover on narrow screens; theme components such as tabs render as static HTML; edge builds stop.
  • react, react-dom, and @farming-labs/theme stay peers. The server uses React for the Markdown; the browser gets none.
  • vitest is an optional peer, for /testing only.

Verification

  • pnpm exec vitest run in packages/farmjs: 26 files, 226 passed.
  • New tests that fail on the previous code:
    • The non-React default in config.test.ts.
    • The renderer handoff in server.test.ts.
    • The hydration script in the head (checked by removing the line that writes it).
    • server-react-vite.test.ts: the theme got the aliased createElement. Its second test fails if the hook runs for an app without the alias (checked by forcing it on).
    • server.test.ts: Solid's root render id on the docs root (failed before the fix).
  • tsc --noEmit: clean. oxfmt --check and oxlint on the changed files: clean.
  • Conformance, 5/5 each: the reference renderer, React, Preact, Vue, Svelte, Solid, and leaf.
    • Solid ran in two parts: server output from a node run, then hydration with Solid's real hydration script and hydrationAttributes: ["data-hk"].
    • A renderer that renders instead of hydrating fails it.
  • Real apps, with packed tarballs of this branch and farm.js#3144, in installed Chrome:
    • leaf (farming-labs/leaf): farm-leaf's tests 31/31, and e2e 17/17 against farm dev and the production build. The 4 docs tests cover server rendering by leaf, hydration that keeps the server's DOM, the theme toggle, page links, and the mobile sidebar. The production client JS contains no React; the docs runtime is one 4.2 KB chunk over the wire. Checked by eye at 1280px and 390px, with no horizontal scroll.
    • Copies of farm.js's Preact, Solid, Vue, and Svelte examples with withDocs() and leaf's docs pages: leaf's 4 docs tests pass against farm dev and the production build for each, 32/32. Preact dev failed 2 of them before the react alias fix.
    • leaf again on this branch's head: 31/31 and e2e 17/17 against farm dev and the production build.
    • Serverless: the Preact copy built for deploy: { target: "vercel" }, its function's fetch handler served locally with static/: leaf's 4 docs tests pass.
    • With the table of contents (head bcd96d54): the docs e2e gains a test that the server writes #nd-toc, lists the sections, and marks the one a link scrolls to. Preact, Solid, Vue, and Svelte copies against farm dev and the production build, plus the Vercel output: 45/45. Solid failed 1/5 (prod) and 0/5 (dev) before the render id fix. leaf: 31/31, e2e 18/18 against farm dev and the production build. Checked by eye at 1440px (dark and light) and 390px, with no horizontal scroll.

Not verified:

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

Documentation

  • README: "Custom renderers" covers the universal default, the conformance suite, a UI of your own, and the limits. ./universal and ./testing join the entrypoints table.

Summary by cubic

Farm apps can now render docs with a UI built for any Farm renderer instead of being limited to the package's React UI. Apps on another renderer get docs from withDocs() alone through the new @farming-labs/farmjs/universal UI, or can name their own UI with withDocs({ ui: { module, renderer } }). Existing React apps keep the same rendering, and the server loads React only when the UI uses it.

  • The universal UI builds each page with the app's renderer, keeps the React UI's layout and stylesheet, and runs folders, the theme toggle, the mobile sidebar, and copy buttons with plain DOM listeners. Markdown compiles with React on the server only; the browser export keeps React and compiled pages out of the client bundle.
  • The server passes the app's renderer to the UI and writes its hydration script into the head. @farming-labs/farmjs/testing verifies a renderer writes and hydrates docs pages as expected.
  • In development, a Vite plugin ordered before the app's alias ensures docs pages render with the real React when the app points react at another library, so the Markdown's React components never mix with the app's.
  • Custom UIs export renderFarmDocsPage() and hydrateFarmDocs(); createFarmDocsNavigator() from @farming-labs/farmjs/navigation provides client-side page navigation.
  • The universal UI has no search box, table of contents, or client navigation. Custom UIs need a Farm release that reads ui and passes renderer, must render rawContent themselves, and cannot use shell or edge rendering.