Repository navigation
Done
Description
Environment
- h3-compression: 1.1.0 (and
mainatdab4d75) - h3: 2.0.8
- Node.js: v26.13.0
Reproduction
// npm install h3-compression@1.1.0 h3@2.0.8 && node repro.mjs
import { H3, mockEvent } from "h3";
import { compression, compressionStream, useCompression } from "h3-compression";
for (const [name, mw] of [
["compression()", compression()],
["compressionStream()", compressionStream()],
]) {
const app = new H3();
app.use(mw);
app.get("/", () => "Hello world! ".repeat(100));
for (const ae of ["br;q=0, gzip", "gzip;q=0", "gzip;q=0.1, deflate;q=0.9"]) {
const res = await app.fetch(new Request("http://localhost/", { headers: { "accept-encoding": ae } }));
console.log(`${name} | ${ae} -> content-encoding: ${res.headers.get("content-encoding")}, vary: ${res.headers.get("vary")}`);
}
}
const event = mockEvent("/", { headers: { "accept-encoding": "br;q=0, gzip" } });
const response = { body: "Hello world! ".repeat(100) };
await useCompression(event, response);
console.log(`useCompression() | br;q=0, gzip -> content-encoding: ${event.res.headers.get("content-encoding")}, vary: ${event.res.headers.get("vary")}`);Describe the bug
getAnyCompression, getStreamCompression, compress, and compressStream in src/helper.ts check Accept-Encoding with encoding?.includes(method), and neither compress / compressStream nor cloneResponse sets Vary: Accept-Encoding.
- Encodings explicitly rejected with
q=0are still selected:Accept-Encoding: br;q=0, gzipreturnsContent-Encoding: brundercompression()anduseCompression(), andAccept-Encoding: gzip;q=0returnsContent-Encoding: gzipunder bothcompression()andcompressionStream(). - Quality weights are ignored:
Accept-Encoding: gzip;q=0.1, deflate;q=0.9returnsContent-Encoding: gzipinstead ofdeflate. Varyisnullon compressed responses, so shared caches and CDNs can serve a compressed response cached for one request to clients that did not request that encoding.
Expected per RFC 9110 (https://www.rfc-editor.org/rfc/rfc9110#section-12.5.3):
br;q=0, gzipselectsgzip,gzip;q=0does not compress withgzip, andgzip;q=0.1, deflate;q=0.9selectsdeflate.- Responses whose representation depends on
Accept-EncodingincludeAccept-Encodingin theVaryheader (preserving any existingVaryvalues).
Additional context
This affects both the h3 v2 middleware (compression, compressionStream) and the Nitro/composable helpers (useCompression, useCompressionStream, and the h3-compression/nuxt module).
Logs
compression() | br;q=0, gzip -> content-encoding: br, vary: null
compression() | gzip;q=0 -> content-encoding: gzip, vary: null
compression() | gzip;q=0.1, deflate;q=0.9 -> content-encoding: gzip, vary: null
compressionStream() | br;q=0, gzip -> content-encoding: gzip, vary: null
compressionStream() | gzip;q=0 -> content-encoding: gzip, vary: null
compressionStream() | gzip;q=0.1, deflate;q=0.9 -> content-encoding: gzip, vary: null
useCompression() | br;q=0, gzip -> content-encoding: br, vary: null
Credit: https://ucsbplse.github.io/
Metadata
Metadata
Assignees
Labels
No labels
Activity
CodeDredd commentedon Nov 24, 2026
Thanks for the precise report and the RFC references — fixed in #28, it will ship with the next release.
Accept-Encodingis now parsed per RFC 9110 instead of matched by substring:q=0rejects a coding:br;q=0, gzip→ gzip,gzip;q=0→ no compression,gzip;q=0.1, deflate;q=0.9→ deflate.*covers every coding not listed, coding names are case-insensitive, andx-gzipcounts as gzip.Every negotiated response now carries
Vary: Accept-Encoding— compressed or not — and existingVaryvalues are preserved (Origin→Origin, Accept-Encoding,*is left alone). That applies tocompression()/compressionStream(),useCompression()/useCompressionStream()and theh3-compression/nuxtmodule.One deliberate exception: a method forced on the middleware (
compression('gzip')) is documented as "always", ignoresAccept-Encodingand therefore doesn't addVary. The forced composables (useGZipCompression(), …) do checkAccept-Encoding, so they now respectq=0as well.Your reproduction is part of the test suite for both middlewares, the composables and the h3 v1 app hook.
CodeDredd commentedon Nov 24, 2026
Released in v1.2.0 —
npm install h3-compression@1.2.0.With your reproduction on 1.2.0 the results are now
br;q=0, gzip→gzip,gzip;q=0→ no compression,gzip;q=0.1, deflate;q=0.9→deflate, each withVary: Accept-Encoding. Thanks again for the report!