perf(docs): preload a docs page's modules and load the docs config alongside the adapter

Merged
KI
1mo ago
+416-12

Problem

a docs page's browser code loads as a chain: the html names only the client entry, the entry dynamically imports the docs adapter, the adapter's runtime then imports the docs config, and the adapter then imports the page's compiled markdown. each step is discovered only after the previous one downloads and runs. on the assistant-ui docs port (376 pages) the page hydrated ~60-100 ms after next.js over a real network, with next's chunks all listed up front.

waterfall at 40 ms rtt, before: html 0-52, entry 57-110, adapter + react 120-197, docs config 219-261, page chunk 262-304, hydrated ~350 ms.

Change

  • the generated client entry imports the docs adapter and the docs config together (Promise.all) instead of one after the other. they don't depend on each other.
  • the client build records, for a react app with a docs adapter, the chunks every docs page needs at startup (the entry, the adapter and the docs config with their static imports) and each compiled markdown page's own chunks (.farm/docs-module-preloads.json, not in the public dir).
  • the server bundle can't read that at its own build (client and server build in parallel), so it carries a placeholder the build fills in afterwards, the same way the client entry's hashed name is filled in. an unfilled placeholder parses to nothing.
  • the production adapter handler gets modulePreloads: { runtime, pages }. adapters that support it (farming-labs/docs, separate pr) list them as <link rel="modulepreload">; older adapters ignore it.

Safety and compatibility

  • only production builds of react apps with a docs adapter. dev, other renderers, apps without docs and edge's precompiled manifest are unchanged.
  • preloads only name chunks the page loads anyway: same 10 script requests and same bytes before/after in chrome.
  • the manifest is keyed by the client build's module path; the adapter matches it to a page the way the browser finds that page's module, since the server's source path can be content-dir-relative (it is on vercel).
  • a client build without the adapter chunk writes null, so a rebuild never serves a stale list.

Verification

  • new docs-module-preloads.test.ts: runtime vs page chunk lists, no adapter chunk -> null, encoding round-trips in ", ' and template literals (incl. $, backticks, u2028), plugin writes and later overwrites the manifest. docs-adapter-client.test.ts: config and adapter are both requested before either loads. docs-adapter.test.ts: production handler gets modulePreloads.
  • docs suites 81 tests, production docs/vercel/owned-response/sync-render 26 tests, core type-check, oxlint, oxfmt.
  • assistant-ui port, same build with vs without the links (chrome, interleaved, 7 runs, 40 ms emulated rtt): hydrated 336 -> 223 ms (architecture), 348 -> 246 ms (api reference). same bytes, no new requests or errors.
  • on vercel (same deployment, html variants interleaved, ~80-200 ms real rtt): hydrated 593 -> 469 ms and 689 -> 396 ms with all chunks preloaded; first paint within noise (324 -> 340, 368 -> 328 ms). preloading only the tail or using fetchpriority=low did worse.
  • build time unchanged (36.8 s local).

Documentation and release

none. pairs with the adapter pr; needs a release for apps to get it.


Summary by cubic

Speeds up docs page hydration by fetching the docs config alongside the docs adapter and preloading the chunks a page needs.

Before, a docs page's browser code loaded as a chain: the client entry, then the docs adapter, then the docs config, then the page's compiled Markdown — each step discovered only after the previous one downloaded and ran. The generated client entry now imports the adapter and config in parallel, and the production docs handler receives a modulePreloads map so adapters can list the page's chunks as <link rel="modulepreload">.

  • The client build writes a manifest of per-page and shared runtime chunks next to the build output; the server bundle's placeholder is filled in after the client build finishes, and an unfilled placeholder reads as null.
  • Only production builds of React apps with a docs adapter change; dev, other renderers, apps without docs, and the edge precompiled manifest are untouched.
  • Adapters that don't support modulePreloads ignore it.
  • On Vercel, hydration dropped from ~593/689 ms to ~469/396 ms with the same bytes and no new requests or errors; build time is unchanged.