next.js
d15482dc - Send a small in-memory route handler body with Content-Length, uncompressed

Commit
26 days ago
Send a small in-memory route handler body with Content-Length, uncompressed `next start` piped every route handler response as a stream of unknown length, so the compression middleware gzipped all of them, including a body of 19 bytes that grows to 39 when gzipped. Creating and driving a gzip stream costs more CPU than the rest of the request for a small JSON response, and the threshold the middleware has for this (1024 bytes) never applied because it needs a Content-Length. What changes When a route handler's response body arrives as a single chunk of fewer than 1024 bytes and its stream is closed in the same tick, the chunk is the whole body. It is now sent with `res.end(chunk)` and a `Content-Length`, in one write, and the compression middleware's size threshold applies, as it already does for a Pages API route and for the same route handler on a deployment that serves it with a Content-Length. 1024 is the default `threshold` of the `compression` middleware, which is how the router server sets it up. Such bodies are those created from a string, an ArrayBuffer, a typed array or `Response.json()` / `NextResponse.json()`, a cached (prerendered) route handler body, and a `ReadableStream` that enqueues one small chunk and closes synchronously. For these responses a client sees, for GET /api/app in the benchmark app (body `{"name":"John Doe"}`): before, Accept-Encoding: gzip Content-Encoding: gzip, Transfer-Encoding: chunked, 39 body bytes before, no Accept-Encoding Transfer-Encoding: chunked, 19 body bytes after, either Content-Length: 19, 19 body bytes, no Content-Encoding, no Transfer-Encoding `Vary` still includes `Accept-Encoding`, and status, content type, the other headers and the decoded body are the same. A body between roughly 150 and 1023 bytes that compresses well is now larger on the wire, by up to a few hundred bytes: the 331-byte JSON of /api/projects/[id] was 246 bytes gzipped, the 424-byte body of /api/echo was 298. Bodies under about 100 bytes get smaller. What stays the same - A body of 1024 bytes or more, and any body that is still streaming on the next tick (the held chunk is then written and the response is streamed as before). - A response that declares `Content-Length`, `Content-Encoding` or `Transfer-Encoding` (these headers are still not forwarded). - A response with a pending `waitUntil` promise, a HEAD request, a null body. - `compress: false`, a custom server or platform without the compression middleware, minimal mode and the edge runtime: the path is only taken when the middleware is active on the response. - Pages, Server Components and middleware responses: they do not go through `sendResponse`. The two cases of test/e2e/compression that asserted gzip for a route handler body below 1kb on `next start` (added in #101220) are updated to the new behaviour; their expectations for deployments are unchanged. Measured on a base that already detects the OpenTelemetry noop tracer provider and reuses a route module's loaded manifests (4 VMs, 4 pairs, each run the first ~11,000 requests of a freshly started server): /api/app +24.5% ±4.6% requests per second and -25.5% ±1.9% serial median latency, /api/projects/7 +18.8% ±3.7%, /api/echo +25.0% ±3.6%, /api/deployments (26 KB, over the threshold, response unchanged) -0.8% ±0.9%.
Author
Committer
Parents
Loading