perf(core): link no stylesheet when the app has no CSS

Merged
KI
12d ago
+213-46

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 null when farm-client.css is 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 with JSON.parse at runtime, so the minifier cannot fold the placeholder.
  • farm-client.css is still written and served for anything that hardcoded it.
  • first-navigation.mjs --paint times 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 globalStylesheetHref and adapter stylesheets, the edge docs manifest, the mobile shell, and farm 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 run on document-injection and client-css-href: 15 passed. The composer test stubs the helper, and adds a case with no stylesheet.
  • Apps with CSS: pnpm exec vitest run on production-prebuilt-ssr (which checks the stylesheet comes before the modulepreloads), production-immutable-assets, client-module-preloads, docs-module-preloads, vercel-assets and immutable-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-handler and font-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.ts with the installed Chrome channel, every spec except preview.spec.ts: 19 passed. That spec needs farm-preview-gateway built, and it isn't in this checkout.
  • oxfmt --check and oxlint on 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:

Sectionrsc, unlimitedbeforeafterrsc, 1.6 Mbit/sbeforeafter
docs280396268236420288
shop240384240256412248
blog280404264264440268
guide272404276276432272
  • 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 null when farm-client.css is 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.css is 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.