Skip to content

Accept-Encoding q-values are ignored (q=0 still selected) and Vary: Accept-Encoding is not set #25

Done

Description

@trackoor

Environment

  • h3-compression: 1.1.0 (and main at dab4d75)
  • 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.

  1. Encodings explicitly rejected with q=0 are still selected: Accept-Encoding: br;q=0, gzip returns Content-Encoding: br under compression() and useCompression(), and Accept-Encoding: gzip;q=0 returns Content-Encoding: gzip under both compression() and compressionStream().
  2. Quality weights are ignored: Accept-Encoding: gzip;q=0.1, deflate;q=0.9 returns Content-Encoding: gzip instead of deflate.
  3. Vary is null on 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, gzip selects gzip, gzip;q=0 does not compress with gzip, and gzip;q=0.1, deflate;q=0.9 selects deflate.
  • Responses whose representation depends on Accept-Encoding include Accept-Encoding in the Vary header (preserving any existing Vary values).

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/

Activity

  1. CodeDredd commented on Nov 24, 2026

    @CodeDredd
    Owner

    Thanks for the precise report and the RFC references — fixed in #28, it will ship with the next release.

    Accept-Encoding is now parsed per RFC 9110 instead of matched by substring:

    • Quality values decide first and q=0 rejects a coding: br;q=0, gzip → gzip, gzip;q=0 → no compression, gzip;q=0.1, deflate;q=0.9 → deflate.
    • The server preference (zstd → br → gzip → deflate, opt-ins unchanged) only breaks ties.
    • * covers every coding not listed, coding names are case-insensitive, and x-gzip counts as gzip.

    Every negotiated response now carries Vary: Accept-Encoding — compressed or not — and existing Vary values are preserved (Origin → Origin, Accept-Encoding, * is left alone). That applies to compression() / compressionStream(), useCompression() / useCompressionStream() and the h3-compression/nuxt module.

    One deliberate exception: a method forced on the middleware (compression('gzip')) is documented as "always", ignores Accept-Encoding and therefore doesn't add Vary. The forced composables (useGZipCompression(), …) do check Accept-Encoding, so they now respect q=0 as well.

    Your reproduction is part of the test suite for both middlewares, the composables and the h3 v1 app hook.

  2. CodeDredd commented on Nov 24, 2026

    @CodeDredd
    Owner

    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 with Vary: Accept-Encoding. Thanks again for the report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions