perf(theme): load page feedback, the locale control, and sidebar AI on demand

Merged
KI
11h ago
+62-4

Problem

The TanStack layout and the page client import four features statically, so every site downloads them on its first visit, even with the feature turned off:

  • page feedback;
  • the locale control;
  • the sidebar AI search;
  • the AI features.

BrowserDocsLayout is the Farm.js adapter's layout, and it renders through the TanStack layout. The Next.js layout already loads the AI features lazily through docs-ai-features-lazy.

Change

  • tanstack-layout.tsx imports DocsAIFeatures from the existing docs-ai-features-lazy wrapper.
  • A new optional-components-lazy.tsx wraps DocsFeedback, LocaleThemeControl and SidebarSearchWithAI. Each one loads on its first render and sits inside its own Suspense boundary.
  • tanstack-layout.tsx and docs-page-client.tsx import those wrappers.

Safety and compatibility

  • Server render: an enabled feature still renders in the server HTML, in place. The Farm.js adapter awaits allReady, and the new test asserts the feedback section renders once, with no <template id="B: and no $RC.
  • Hydration: in the browser, each boundary keeps the server's HTML until its chunk arrives.
  • Markdown components stay eager. universal-content.tsx renders page Markdown with the synchronous renderToStaticMarkup, which would render a lazy component's empty fallback. I tried making Prompt and HoverLink lazy, and the mdx.test.ts tests that render Prompt failed for that reason, so I left them out.
  • Public exports are unchanged: @farming-labs/theme still exports the eager components.

Verification

  • pnpm exec vitest run in packages/fumadocs: 480 passed.
  • pnpm exec vitest run in packages/farmjs: 244 passed, including the new feedback SSR test.
  • tsc --noEmit passes, and oxlint is clean.
  • farmjs.dev, built locally with this theme (node-server preset):
    • The shared theme chunk shrank from 400,640 to 384,337 bytes raw. With brotli at quality 11 that's 103,999 → 101,803 bytes; with gzip, 126,758 → 123,571.
    • Feedback, the locale control and the sidebar AI search now build as their own chunks of 1.7, 1.6 and 0.7 KB (brotli). farmjs.dev never requests them.
  • The same build with feedback.enabled: true, checked in Chrome:
    • The server HTML has the feedback section, and docs-feedback loads.
    • The button hydrates and works.
    • The section survives client navigation, with no console errors.

Documentation and release

None. This is a small saving, so it can ride along with the next release rather than getting its own.

šŸ¤– Generated with Claude Code


Summary by cubic

Loads page feedback, the locale control, sidebar AI search, and AI features only when a site actually enables them, instead of bundling them into every first visit.

  • The TanStack layout and page client now import these via lazy Suspense wrappers; enabled features still render in the server HTML in place because the Farm.js adapter awaits allReady.
  • Markdown components (Prompt, HoverLink) stay eager since page Markdown is rendered synchronously with renderToStaticMarkup, which wouldn't wait for lazy chunks.
  • Adds an SSR test asserting an enabled feedback section renders once with no suspended-template markers.
  • For farmjs.dev, the shared theme chunk drops from 400,640 to 384,337 bytes raw (104.0 → 101.8 KB brotli).
  • Public exports from @farming-labs/theme are unchanged.