perf(core): link no stylesheet when the app has no CSS
Problem
An app without CSS still links a stylesheet from every document: <link rel="stylesheet" href="/farm-client.css">, an empty file. A stylesheet link blocks rendering and the module scripts after it. So the browser waits a full round trip for zero bytes before its first paint.
On the payload benchmark (an app with no CSS), at 150 ms of latency, every islands page painted about 140 ms after the rsc engine's. The rsc engine links no stylesheet when there is none. Found while measuring RFC 0013 (#3134).
Root cause
validateClientBuildOutput writes an empty farm-client.css for a style-free app, so the stable URL doesn't 404. The generated server code then wrote the link tag literally in eight places: page documents, the streamed shell, error and not-found documents, and docs pages in an app layout. None of them checked whether the stylesheet had anything in it. The existing style-free test only checked that the page and the file were served.
Change
- The build resolves the stylesheet a document links. That is its content-hashed href, or
nullwhenfarm-client.cssis empty (resolveClientStylesheetLinkHref). The value is substituted into the server bundle through a new JSON placeholder, as the module preloads and route table already are. - One generated helper,
renderFarmClientStylesheet(), renders the link or nothing. All eight sites use it. The value is read withJSON.parseat runtime, so the minifier cannot fold the placeholder. farm-client.cssis still written and served for anything that hardcoded it.first-navigation.mjs --painttimes a cold visit's first contentful paint over the same emulated network and HTTP/2 proxy.
No config and no public API.
Safety and compatibility
- An app with CSS links the same href as before. If the stylesheet can't be read at build time, the link is left out; the build validates the file before this step.
- Covers every renderer and both server components engines' universal documents, on every Nitro target.
- Not changed: the docs runtime's own
globalStylesheetHrefand adapterstylesheets, the edge docs manifest, the mobile shell, andfarm dev. These keep linking the stylesheet. Docs apps ship CSS, and dev runs on localhost.
Verification
Regression test: production-no-globals-css.test.ts now builds a default app and an islands-mode app with no CSS. It checks that the page and the not-found page link no stylesheet, and that /farm-client.css is still served. Both tests fail without the change (expected '<!DOCTYPE html>…' not to match /<link\b[^>]*\brel="stylesheet"/i) and pass with it.
Run from packages/farm unless noted:
pnpm exec vitest run src/__tests__/production-no-globals-css.test.ts: 2 passed.pnpm exec vitest runondocument-injectionandclient-css-href: 15 passed. The composer test stubs the helper, and adds a case with no stylesheet.- Apps with CSS:
pnpm exec vitest runonproduction-prebuilt-ssr(which checks the stylesheet comes before the modulepreloads),production-immutable-assets,client-module-preloads,docs-module-preloads,vercel-assetsandimmutable-assets: 130 passed. - Other tests that mention the stylesheet:
full-document,full-document-stream,document-head,metadata-viewport,mobile-*,preload,production-docs-vercel,production-preload-response,docs-handlerandfont-vite: 271 passed. pnpm --filter @farm.js/core type-check: passed.- From the repo root,
FARM_SERVER_COMPONENTS_MODE=islands pnpm exec playwright test -c playwright.server-components.config.ts, production: 147 passed, 8 skipped. - From the repo root,
playwright.isolated-hydration.config.tswith the installed Chrome channel, every spec exceptpreview.spec.ts: 19 passed. That spec needsfarm-preview-gatewaybuilt, and it isn't in this checkout. oxfmt --checkandoxlinton the changed files: clean.
Benchmark, first contentful paint (first-navigation.mjs --paint, 150 ms latency, Apple M1, Chrome 157, 4x CPU, HTTP/2 proxy). Interleaved with rsc, the build from before and an A/A copy of it; medians of 9, ms:
| Section | rsc, unlimited | before | after | rsc, 1.6 Mbit/s | before | after |
|---|---|---|---|---|---|---|
| docs | 280 | 396 | 268 | 236 | 420 | 288 |
| shop | 240 | 384 | 240 | 256 | 412 | 248 |
| blog | 280 | 404 | 264 | 264 | 440 | 268 |
| guide | 272 | 404 | 276 | 276 | 432 | 272 |
- First paint is 128 to 172 ms sooner, and level with or ahead of
rsc, except docs at 1.6 Mbit/s. - A/A noise: within 4 ms on the unlimited run (load average 8 to 10). Within 16 ms on the 1.6 Mbit/s run, which overlapped another session's work (load average up to 56).
- The load event ends about 75 ms sooner too (a separate scratch probe, 7 rounds).
Not verified:
- Windows.
- Deploy targets other than
node-server. - Non-React renderers' e2e suites. The change is in their shared generated document code.
farm dev, which this doesn't touch.
Documentation and release
benchmarks/server-components-payload/README.md: a dated section with the table above.- No docs page describes the link.
Summary by cubic
Stops style-free apps from linking an empty farm-client.css in their documents, so the first paint no longer waits a round trip for zero bytes.
- The build resolves the stylesheet a document links — its content-hashed href, or
nullwhenfarm-client.cssis empty — and injects it through a new JSON placeholder. - One generated helper,
renderFarmClientStylesheet(), renders the link or nothing, and all eight document sites now use it. farm-client.cssis still written and served for anything that hardcoded the URL, and apps with CSS keep their existing link.- Regression tests now cover a default app and an islands-mode app with no CSS; both fail without the change.
Benchmark
On the payload benchmark (an app with no CSS) at 150 ms latency, first contentful paint of an islands page comes 128–172 ms sooner, landing level with the rsc engine.