perf(farmjs): list a docs page's modules as modulepreload links

Merged
KI
11h ago
+84-0

Problem

On Farm.js, a docs page's browser code is discovered one dynamic import at a time: the client entry, then the adapter, then the docs config, then the page's compiled Markdown. Hydration waits on each round trip.

Farm.js core has passed the adapter the chunks a page needs since farming-labs/farm.js#2146 (merged 2026-10-26), but the adapter ignores the modulePreloads option. farmjs.dev's docs pages have no modulepreload links today.

Change

  • createFarmDocsRuntimeHandler takes modulePreloads: { runtime, pages }. runtime holds the public chunk paths every page needs. pages holds each compiled page's chunks, keyed by the client build's source path.
  • The document head lists runtime plus the page's own chunks as <link rel="modulepreload">, deduped and escaped, after the stylesheets.
  • The page's chunks are found with resolveDocsModule, the matcher the browser uses to load the page's module. The server's sourcePath can be relative to the content directory, while the client build's keys keep the app prefix.

Rebased onto main. The original October version conflicted.

Safety and compatibility

  • Without modulePreloads, the head is unchanged; a test covers this.
  • In development Farm.js doesn't pass the option.
  • Paths are HTML-escaped.

Verification

  • vitest run in packages/farmjs: 246 passed. The modulepreload test fails against main's server.ts.
  • tsc --noEmit passes, and oxlint is clean.
  • farmjs.dev, built locally with this adapter on 0.2.162 (node-server preset): /docs/routing lists its 9 runtime chunks plus its page chunk.
  • A/B on that build in Chrome, simulated 4G (150 ms latency, 1.6 Mbps), 8 runs per arm, median time until the sidebar is hydrated. Both arms go through the same Playwright route; the control strips the links.
PageNo preloadsWith preloads
/docs/renderers/react4067 ms3574 ms
/docs/routing3933 ms3536 ms
/docs3748 ms3405 ms

Transferred bytes are identical in both arms.

Documentation and release

None.

šŸ¤– Generated with Claude Code