switchyard: compress direct route.go responses as Node does (#102278)
### About this stack
These PRs let Switchyard, the experimental Go server in front of
self-hosted `next start` (`__NEXT_EXPERIMENTAL_SWITCHYARD=1`, #101695
and #102035), send `route.go` requests straight to their Go worker
instead of through Node. Switchyard goes direct only when Node would
have passed the request to the worker unchanged, so clients get the same
response either way, apart from the differences each PR lists
(compression, #102278, differs from Node's in places); everything else
keeps going to Node. Each PR adds one kind of request that can go
direct. The stack splits #101981 for review; the first two PRs have
merged.
1. **Skeleton (merged)** (#102101): Node tells Switchyard which exact
paths are route.go handlers, and which route.go workers are running.
Every request still goes to Node.
2. **Review fixes for the skeleton (merged)** (#102168): worker-gone
messages name the worker by its token, incomplete worker messages are
ignored, and tests cover the worker events and how the route table is
collected.
3. **Happy path** (#102102): requests to exact route.go paths go
straight to the worker, with any headers, built exactly as Node would
build them.
4. **Query strings** (#102103): requests with a query that Node would
pass on unchanged.
5. **Dynamic segments and catch-alls** (#102113): `/go/users/[id]`,
`[...path]` and `[[...path]]`, matched in the router's order.
6. **→ This PR:** **Compression** (#102278): apps with `compress` on
(the default), compressed the way Node does, so browser requests can go
direct.
7. **basePath** (#102114): apps that set a `basePath`.
8. **Request bodies** (#102115): POST and PUT with a body, including
answers sent before the upload ends.
9. **Early Hints** (#102116): 103 responses are dropped, as Node drops
them.
10. **Worker threads** (#102118): each route.go worker runs Go code on
at most 4 threads by default.
11. **Trailers and the response-header limit** (#102119): trailer values
are dropped and Node's limit on a worker's response headers applies, as
through Node.
12. **Hop-by-hop response headers** (#102120): the worker's
`Keep-Alive`, `Proxy-Authenticate` and similar headers reach the client,
as through Node.
### What?
Apps with `compress` on, which is the default, can now use the direct
path. Requests from browsers and `fetch`, which always send
`Accept-Encoding`, no longer have to go through Node.
### Why?
Until now an app with `compress` on got no direct routes, because Node's
compression middleware (`compression` 1.7.4) gzips responses and
Switchyard didn't.
It sits right after dynamic routes, before `basePath`, request bodies
and Early Hints, because until it lands a default app gets nothing from
the direct path. It replaces #102117, which had the same change further
up the stack.
### How?
- Direct responses are compressed with gzhttp
(`github.com/klauspost/compress/gzhttp`), wrapped around the direct path
when Node's route table says `compress` is on. Switchyard doesn't copy
the middleware's choices one for one.
- Kept from Node's middleware: the 1 KiB threshold for a body of known
length, and no compression for HEAD, for a response without a
`Content-Type`, for `Cache-Control: no-transform`, or for a response
that already has a `Content-Encoding`. gzhttp only sends gzip here; its
zstd is turned off, since the middleware never sends it.
- Which types are compressed is a short rule in `compress.go` (text,
JSON, XML and JavaScript types, their `+json`/`+xml`/`+text` suffixes,
and the fonts, icons, WebAssembly and form types the database marks
compressible), not the middleware's MIME database.
- A direct request handed back to Node (its worker couldn't be reached)
gets Node's answer as Node wrote it: the compressor leaves the body and
its `Vary` alone.
- New dependency: `github.com/klauspost/compress` v1.19.2, the newest
release that builds with Go 1.24, which Switchyard's `go.mod` and CI
use.
#### Differences from Node's compression
Also listed at the top of
`packages/next/src/server/switchyard/compress.go`. A request that goes
through Node is compressed by Node as before; these apply only to direct
responses.
| Case | Node's middleware | Switchyard (gzhttp) |
| --- | --- | --- |
| `Vary: Accept-Encoding` | only on compressible types, folded into the
worker's `Vary` (`Origin, Accept-Encoding`) | on every response, as its
own line |
| Client accepts deflate but not gzip | deflate | uncompressed |
| `Accept-Encoding: *` | gzip | uncompressed (gzhttp's wildcard option
needs v1.20, which needs Go 1.25) |
| Client prefers identity (`gzip;q=0.5, identity`) | uncompressed | gzip
|
| Worker sends `Content-Encoding: identity` | replaced, and compressed |
left as it is |
| Which types count as compressible | the middleware's MIME database |
the rule in `compress.go`, which covers all but 21 of the database's 607
compressible types (such as `application/x-sh`, tar archives and
VirtualBox disk images) |
| Empty body of unknown length | chunked | `Content-Length: 0` |
| Streamed chunks | held in zlib until more output is ready | sent as
the worker flushes them |
The compressed bytes differ in any case; the decompressed body is the
same.
### Verification
- `go test -race ./...` in `packages/next/src/server/switchyard`: gzip
for a compressible body of at least 1 KiB, the threshold, PNG, HEAD,
`no-transform`, `Vary`, an empty body, a stream held in the compressor,
and an aborted stream.
- `switchyard-go` and `switchyard-go-direct` with
`__NEXT_EXPERIMENTAL_SWITCHYARD` off and on: 85 of 85 both ways. The
cases that record Node's middleware expect gzhttp's value where the
request goes to the worker directly.
- The other Switchyard suites (`switchyard`, `switchyard-process`,
`native-route-go`) and `next-server-nft` pass, except "keeps external
callers out of loopback-only API routes", which connects to the
machine's own network address and fails locally with Switchyard off too;
CI runs it.
- `pnpm --filter=next types`, `pnpm typescript`, ESLint on the changed
files; the Switchyard unit tests (61 of 61).
- Not re-measured: the benchmark in the current description was taken
with the copied middleware.
<!-- NEXT_JS_LLM -->
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>