feat(farmjs): render docs on any Farm renderer without a custom UI
Merged
KI
12d ago+2098+2098+0123456789012345678901234567890123456789-41-41-01234567890123456789
Problem
@farming-labs/farmjsrenders 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, whererenderPage()rendersFarmDocsPage.
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 exportsrenderFarmDocsPage()andhydrateFarmDocs(), typed byFarmDocsUIModule.- The server loads the UI through Farm's
loadUIModule(), falling back toloadReactModule(). A UI that exportsFarmDocsPagestill renders through React, which loads only then. - The descriptor carries
uiandrenderer. The React UI keepsreactfor Farm releases that read only that field; another UI dropsreactandedgeCompiler. - 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/universalUI. It builds each page from host elements with therendererFarm passes in (farm.js#3144):createElement()andrenderToString()on the server,createElement()andhydrateRoot()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
browserexport 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 withuiat once, and says whyshellhas nothing to wrap on another renderer.- In development, an app that aliases
react(Preact's preset points it atpreact/compat) no longer splits the Markdown render between two libraries. AresolveIdhook 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-toccolumn and classes. The server reads them from the Markdown's HTML; the browser reads the list back from the markup. AnIntersectionObservermarks the headings on screen withdata-active. It followstheme.ui.layout.toc.enabledand shows on wide screens only. - On Solid,
#farm-docs-rootgetsdata-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 asa600(the 61st element) reads as render ida6, so hydration fails. The table of contents made the page exactly that long.
Safety and compatibility
- React apps render the same way.
browser.cssgains 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/themestay peers. The server uses React for the Markdown; the browser gets none.vitestis an optional peer, for/testingonly.
Verification
pnpm exec vitest runinpackages/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 aliasedcreateElement. 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).
- The non-React default in
tsc --noEmit: clean.oxfmt --checkandoxlinton 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.
- Solid ran in two parts: server output from a node run, then hydration with Solid's real hydration script and
- 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 devand 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 againstfarm devand the production build for each, 32/32. Preact dev failed 2 of them before thereactalias fix. - leaf again on this branch's head: 31/31 and e2e 17/17 against
farm devand the production build. - Serverless: the Preact copy built for
deploy: { target: "vercel" }, its function'sfetchhandler served locally withstatic/: 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 againstfarm devand 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 againstfarm devand the production build. Checked by eye at 1440px (dark and light) and 390px, with no horizontal scroll.
- leaf (farming-labs/leaf): farm-leaf's tests 31/31, and e2e 17/17 against
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.
./universaland./testingjoin 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
browserexport 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/testingverifies 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
reactat another library, so the Markdown's React components never mix with the app's. - Custom UIs export
renderFarmDocsPage()andhydrateFarmDocs();createFarmDocsNavigator()from@farming-labs/farmjs/navigationprovides 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
uiand passesrenderer, must renderrawContentthemselves, and cannot useshellor edge rendering.