fix(theme): render a page's description and title actions on the server when it has its own h1

Merged
KI
11h ago
+390-4

Rebased on main (2026-12-05)

This PR was rebased onto current main. That required two follow-ups:

  • Plugin registration: the remark plugin list moved from vite.ts to mdx-compiler.ts, so remarkDocsTitleSlot is registered there now.
  • Context sharing: DocsTitleSlotContext now lives in a Symbol.for registry on globalThis. farmjs.dev imports the layout through an alias (@farming-labs/theme-runtime/browser) and the Markdown components through @farming-labs/theme/mdx. farm dev loads those as two module instances on the server, so the slot rendered nothing there and hydration failed. New test: title-slot.test.ts, which fails without the fix.

One test expectation changed. server-react-vite.test.ts now expects the \n that MDX puts between the h1 and the empty title slot when no theme component renders it.

Verification on farmjs.dev, with this branch overlaid on 0.2.162

Production build (node-server preset), compared in Chrome with live farmjs.dev:

PageLive CLS, desktop / mobileThis branch, desktop / mobile
/docs/renderers/react0.039 / 0.1240 / 0
/docs/routing0.024 / 0.0610 / 0
/docs0.038 / 0.1240 / 0
  • The server HTML has the slot and no fd-title-decorations-fallback.
  • After hydration, screenshots of all six page and viewport pairs are pixel-identical to live.
  • The elements after the h1 are the same as live, through client navigation and Back.
  • "Copy page" copies the page, and "Open in" opens its 8 providers.
  • No console errors.

farm dev: hydration on all three pages has no warnings or errors, cold and warm.

Tests:

  • theme: 485 passed;
  • farmjs: 251 passed;
  • tsc, oxfmt and oxlint are clean.

Problem

when a docs page writes its own # Title (farmjs.dev, most real docs), the theme can't tell where that h1 lands in the rendered mdx. so the description and the below-title block (last updated, page actions) are rendered into a hidden fd-title-decorations-fallback div at the end of the page, and after hydration a layout effect creates a host under the h1, portals the block into it, and keeps a MutationObserver on the whole page.

effects:

  • the description and actions aren't there until js runs (on the assistant-ui port the description paint moved the largest contentful paint from ~100 ms to 168 ms)
  • the content jumps down when the block appears: cls 0.088 on a typical page on mobile, 0.128 on the starter's /docs, above the 0.1 "good" line
  • every page carries a whole-subtree mutation observer

Change

decide the position when the markdown is compiled, render it on the server.

  • farmjs: remarkDocsTitleSlot puts a title slot right after a page's h1, and a second one after the paragraph that follows it (where the description sits when the body already starts with it). only when the h1 is the first visible block; frontmatter, imports and comments before it are skipped. any other page gets no slot.
  • the slot is an mdx expression, ((Slot) => Slot ? <Slot position="title" /> : null)(props.components?.DocsTitleSlot). it reads the component from the page's component map instead of importing the theme, and binds it to a local name because mdx guards jsx that names props.components.* directly and throws when it's missing. so an older theme or a custom component map without the slot renders exactly what it rendered before.
  • theme: DocsTitleSlot (in getMDXComponents) fills whichever slot matches descriptionInBody from a context DocsPageClient provides. it renders the same .fd-title-decorations-host wrapper the portal created, so css and dom order are unchanged.
  • DocsPageClient gets titleSlot (threaded through TanstackDocsLayout, set by the farmjs page renderer). with it on: no hidden fallback, and the layout effect skips the portal when the page has a slot. a page without a slot (h1 not first) still gets the client placement, so nothing disappears.

next and tanstack adapters don't set titleSlot and don't run the plugin, so they're unchanged. they can opt in with the same two lines later.

Safety and compatibility

checked on a fresh create-app starter with farmjs.dev's settings (page actions and last updated below the title) and pages for each shape: frontmatter description, description in body, no h1, a note before the h1, plus the starter's own page.

  • after hydration, all 15 screenshots (5 pages x desktop light/dark + mobile) are pixel-identical before vs after, and the elements after the h1 are the same on every page
  • with javascript off, the description, last updated and actions now show under the title (before: hidden)
  • cls on pages with a slot goes to 0 (/docs mobile 0.128 -> 0.009, frontmatter page 0.088 -> 0)
  • page actions still work after hydration: copy page puts the page on the clipboard, open in opens its menu
  • client navigation across slot, fallback and no-h1 pages keeps one block in the right place, stays same-document, back works, no console errors
  • farm dev: every page hydrates (react attached to the action buttons) with zero console warnings, so no hydration mismatch
  • version skew, since farmjs takes theme and docs as * peers: new theme + old adapter and old theme + new adapter both behave exactly like today (same placement and cls, no errors, hidden fallback still there)
  • PageActions only touches window/document/navigator in handlers and effects, and is already server-rendered on generated-title pages

Verification

  • theme: 289 tests (4 new: slot on server with no fallback, description-position slot, generated-title pages never fill a slot, no titleSlot keeps the fallback)
  • farmjs: 139 tests (5 new: placement rules, list-after-h1, no _missingMdxReference, real render with no components / a map without the slot / with the slot, pages whose first block isn't an h1)
  • docs 1,599, next 181, tanstack-start 4 pass; typecheck clean for theme, farmjs, next, tanstack-start; oxlint and oxfmt clean
  • the farmjs mdx compile cache fingerprints the adapter's implementation files, so cached compiles from before this change are not reused

Documentation and release

none. needs a release; farm's templates pick it up with a pin bump.

šŸ¤– Generated with Claude Code