fix(plugin): compress RSC pages and payloads on Node like other engines

Merged
KI
14d ago
+97-6

Problem

An app on the rsc engine (experimental.serverComponents: { mode: "rsc" }) sends its pages, Flight payloads and API responses uncompressed from a Node server, whatever the browser accepts and although compress is on by default. Farm's other servers (the classic and islands engines) answer with Brotli or gzip.

On benchmarks/server-components-payload, an RSC blog post is 379.5 KB of HTML and its navigation payload 203.7 KB, against 17.9 KB and 10.8 KB with Brotli. At 1.6 Mbit/s (Lighthouse's mobile profile), an uncompressed RSC blog navigation takes over a second.

Root cause

Core compresses in its generated server handler (compressFarmResponse from @farm.js/core/internal/compression-runtime, when compress is on and the target runs Node). The rsc engine's server is a different entry, generated by @farm.js/plugin (rsc/entries/rsc.ts) and wrapped by its own Nitro build, and neither compresses. Core's RSC pipeline never handed the plugin the app's compress setting either. Nitro compresses public assets at build time, not dynamic responses.

Change

  • Core's RSC pipeline passes compress to the plugin's config hook, beside redirects, rewrites and headers.
  • The plugin sets compress on the entry context for a build on a Node target unless the app's compress is false, deciding the target with core's own resolveFarmInstrumentationRuntime, as core does.
  • The generated entry then wraps its exported fetch with core's compressFarmResponse. The server's own calls to handler() (local API requests, native-tree renders) still read plain bodies.

Both engines now share one compression implementation: Brotli quality 5 or gzip from Accept-Encoding, per-chunk flushes so streamed pages and Flight still stream, Vary: Accept-Encoding, weak ETags, and no pass over responses that are already encoded, binary or no-transform.

Safety and compatibility

  • No new config. compress: false turns it off, as for the other engines.
  • Development is unchanged: only a build sets it.
  • Workers, edge and Bun targets are unchanged, matching core's rule.
  • Prerendering is unaffected: Nitro's prerenderer calls the app in process with only an x-nitro-prerender header, so its responses stay identity and the written files stay plain.
  • The RSC pipeline rejects routeRules, so no Nitro cache rule can store a compressed response (the case core skips compression for).
  • Responses that vary on Accept-Encoding now say so, which a CDN keys on.

Verification

  • @farm.js/plugin: pnpm exec vitest run src/rsc/core-external.test.ts -t "cache-variants" builds a real RSC app, wraps it in Nitro (node-server), boots it and now also checks that / comes back as br for text/html and gzip for text/x-component with the page intact, identity when asked, and Vary naming Accept-Encoding. Fails without the change (Vary is origin, accept), passes with it.
  • @farm.js/plugin: -t "compresses the production server": the config hook emits the compressing export for a Node build, and not with compress: false, for cloudflare-pages, or for the dev server. The Node case fails without the change; all four pass with it.
  • @farm.js/core: pnpm exec vitest run src/__tests__/rsc-pipeline.test.ts -t "compress setting": fails without the change (compress is undefined in the pipeline config), passes with it.
  • pnpm exec vitest run src/rsc in @farm.js/plugin: 462 passed, 2 failed: cache-variants and config-middleware (real builds) hit the 120 s test timeout while the machine ran at a load average of 42 to 45 from other builds, with no assertion failing. Rerun alone (-t "cache-variants|config-middleware"), both pass.
  • tsc --noEmit for @farm.js/core and @farm.js/plugin: clean. oxfmt --check and oxlint on the changed files: clean apart from three unused-binding warnings already on main.

Not run: the e2e suites locally, and a deployment to Vercel, where core's servers already compress the same way.

Documentation and release

None. The docs never said the rsc engine was uncompressed, and compress itself is undocumented for every engine.


Summary by cubic

Compresses RSC pages, Flight payloads, and API responses served from Node so the rsc engine answers with Brotli or gzip like Farm's other engines.

Core now passes the app's compress setting to the plugin, which turns on entry compression for production builds on Node targets. The generated entry wraps its fetch export with core's compressFarmResponse, so both engines share one implementation: Brotli or gzip by Accept-Encoding, per-chunk flushes, Vary: Accept-Encoding, weak ETags, and no pass on already-encoded or binary responses.

Compatibility

  • compress: false disables it, as for the other engines.
  • Development, Workers, edge, and Bun targets are unchanged.
  • Nitro prerendering keeps writing plain files since its in-process requests send no Accept-Encoding.

Verification

  • A real-build test now checks br, gzip, and identity responses; it fails without the change.
  • Config-hook and core pipeline tests fail without the change and pass with it.