vite-ssr-boost 8.8.2: precompressed files and faster streaming Brotli for SSR on node:http
vite-ssr-boost adds SSR and streaming to an existing Vite + React Router app in Data mode, without switching to Framework mode. Version 8.7 gave it a production server on plain node:http without Express.
Version 8.8 (use 8.8.2) adds compression to that server, in two parts:
- static assets are compressed at build time and served as files;
- SSR documents are compressed while they stream, with Brotli or gzip.
Express stays the default managed server and nothing changes for it: it keeps its compression middleware and ignores precompressed files. Precompression is opt-in. Document compression is on when you run the managed node server (managedServer: 'node') and off in the adapters until you turn it on.
Static files: compress at build time
The plugin's compress option writes a .br (quality 11) and a .gz (level 9) file next to eligible client assets after ssr-boost build:
SsrBoost({ managedServer: 'node', compress: true, });
Or with explicit settings:
SsrBoost({
compress: {
algorithms: ['br', 'gzip'], // default
threshold: 1024, // bytes; smaller files get no variant
include: ['js', 'mjs', 'css', 'html', 'svg', 'json', 'txt', 'xml', 'wasm'], // default
},
});
Build behaviour:
- A variant is kept only when it is smaller than the original.
- An existing variant is kept if it decodes to the file, so a CDN step or another Vite plugin can produce its own. A stale one is rebuilt, and one that no longer pays off is removed.
- Each variant is written to a temporary file in the output root and hard-linked into place, so a build killed halfway leaves nothing inside a served folder. Where hard links are not available the file is copied instead, so do not serve the directory while that build runs.
- The build records the algorithms in
server/ssr-boost.json, andssr-boost startwith the node server picks them up. Passstatic: { precompressed: [] }to serve only the originals.
At request time the server picks the variant from Accept-Encoding (br wins at equal q), keeps the original Content-Type and cache policy, and sends Vary: Accept-Encoding with a weak ETag that names the encoding. Range requests always get the original file, so byte ranges stay valid. No CPU is spent on compression per request.
The numbers, from PR #83: autocannon, 10 connections, 8 seconds, serving react-dom-client.production.js (625,168 bytes). The baseline is the compression package 1.8.2 with br quality 4 and gzip level 6.
| asset, browser Accept-Encoding | bytes | req/s | p99 ms | CPU ms/req |
|---|---|---|---|---|
| compression, br on the fly | 115,138 | 302 | 82 | 5.42 |
| 8.8, precompressed br | 89,698 | 1,388 | 13 | 0.271 |
| asset, gzip only | bytes | req/s | p99 ms | CPU ms/req |
|---|---|---|---|---|
| compression, gzip on the fly | 111,376 | 194 | 91 | 9.06 |
| 8.8, precompressed gzip | 110,507 | 1,077 | 17 | 0.322 |
The br file is 22% smaller because quality 11 is affordable once at build time. The rest of the gain comes from not compressing on every request.
Documents: streaming Brotli and gzip
Rendered HTML cannot be precompressed, so the node transport compresses it as it streams. The managed node server has it on. On your own server you turn it on in the adapter:
import http from 'node:http';
import adapterNode from '@lomray/vite-ssr-boost/adapters/node';
http.createServer(adapterNode(handler, { compression: true }));
compression also takes { format, threshold, brotliQuality, gzipLevel }, and the options are checked when the adapter is created, so a bad value throws at startup. The Express and Fastify adapters accept the same option.
Two details for streaming:
- The encoder flushes once per event-loop turn. The shell reaches the browser while Suspense data is still loading, and a burst of small React writes becomes one compressed block.
- Some responses are left alone: HEAD and bodiless statuses, partial content, responses already encoded or marked
no-transform, event streams and types that do not shrink. Bodies under the threshold are sent as is.
A correction to 8.8.0
The 8.8.0 notes said streaming br for documents was at p99 parity with the compression package. That was wrong. An app's production bench (Docker --cpus=2 --memory=1g, autocannon -c 50 -d 30) measured the 8.8.0 document br p99 at about 3x the compression package's.
8.8.2 fixes it:
- The compressed body goes from the zlib stream straight into the socket with Node backpressure, without a round trip through a Web stream. A body that a later layer rewrapped still goes through the Web reader.
- Brotli uses a 512 KiB window instead of the 4 MiB default. Under concurrent load each encoder holds far less native memory, which removed the full garbage collections the large window caused. Documents up to 512 KiB compress to the same size within a few bytes; larger bodies come out about 1% bigger.
The bench from PR #85: autocannon with 50 connections for 30 seconds, browser Accept-Encoding, server pinned to 2 CPUs with taskset, NODE_ENV=production, V8 heap capped at 768 MB, Node 22.23.2. The page is a synthetic React streaming SSR document of about 11.5 KB compressed. Three rounds with the cases alternating; medians below.
| document | req/s | p99 ms | CPU ms/req | peak RSS MB |
|---|---|---|---|---|
| node:http + compression, br q4 | 173.87 | 430 | 9.11 | 518.4 |
| 8.8.0, br | 153.64 | 386 | 10.30 | 521.9 |
| 8.8.2, br | 193.77 | 315 | 7.34 | 225.9 |
| 8.8.2, gzip | 203.94 | 286 | 6.77 | 115.2 |
Against node:http with the compression package, 8.8.2 br cut p99 from 430 to 315 ms (27%). Throughput rose 11% and CPU per request fell 19%. A hand-written Node pipe (renderToPipeableStream into createBrotliCompress into the response) measured in the same run was within noise of 8.8.2.
Brotli trade-offs in this test:
- Brotli responses were 1.4% larger than with the compression package, because the kit flushes once per turn so the shell leaves early.
- Time to first byte with br is about 1 ms higher than with gzip. That is the Brotli encoder's setup cost.
- Peak memory with br is about twice gzip's. If memory is your limit, use
compression: { format: 'gzip' }.
The bench ran on a shared host with taskset and a heap cap, outside a container, and the app is synthetic. Compare rows within this table only.
Behaviour change on the edge adapter
Before 8.8.0 the edge adapter also encoded images and bodies under 1 KiB. It now skips types that do not shrink and bodies below the threshold. compression: { threshold: 0 } restores the old behaviour for small bodies.
Upgrade
npm i @lomray/vite-ssr-boost@^8.8.2
Then set managedServer: 'node' and compress: true in the plugin, or pass compression to the adapter you already use.
Release notes for 8.8.2 Plugin compress option Precompressed files in serveStatic Repository on GitHub
Comments
No comments yet. Start the discussion.