perf(farmjs): list a docs page's modules as modulepreload links
Merged
KI
11h ago+84+84+01234567890123456789-0-0-0123456789
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
createFarmDocsRuntimeHandlertakesmodulePreloads: { runtime, pages }.runtimeholds the public chunk paths every page needs.pagesholds each compiled page's chunks, keyed by the client build's source path.- The document head lists
runtimeplus 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'ssourcePathcan 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 runin packages/farmjs: 246 passed. The modulepreload test fails against main'sserver.ts.tsc --noEmitpasses, and oxlint is clean.- farmjs.dev, built locally with this adapter on 0.2.162 (node-server preset):
/docs/routinglists 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.
| Page | No preloads | With preloads |
|---|---|---|
| /docs/renderers/react | 4067 ms | 3574 ms |
| /docs/routing | 3933 ms | 3536 ms |
| /docs | 3748 ms | 3405 ms |
Transferred bytes are identical in both arms.
Documentation and release
None.
š¤ Generated with Claude Code