Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,27 @@ createServer(toNodeHandler(app)).listen(process.env.PORT || 3000)
You can also force a specific method (e.g. `compression('gzip')`) instead of detecting it from the
`Accept-Encoding` header.

## Content negotiation

The encoding is picked from the `Accept-Encoding` header as described in
[RFC 9110](https://www.rfc-editor.org/rfc/rfc9110#section-12.5.3):

- Quality values decide first: `gzip;q=0.1, deflate;q=0.9` picks deflate, and an encoding sent
with `q=0` is never used (`br;q=0, gzip` picks gzip, `gzip;q=0` compresses nothing).
- Among equally weighted encodings the server preference applies — zstd (opt-in), brotli, gzip,
deflate.
- `*` stands for every encoding not listed explicitly, and `x-gzip` counts as gzip.
- Without an `Accept-Encoding` header nothing is compressed.

Every response whose encoding was negotiated gets `Vary: Accept-Encoding` — compressed or not —
so shared caches and CDNs never serve a compressed body to a client that didn't ask for it.
Existing `Vary` values are kept. A method forced on the middleware ignores `Accept-Encoding`, so
it doesn't add `Vary`; the forced composables (`useGZipCompression`, …) still only compress when
the client accepts that encoding.

Responses that already carry a `Content-Encoding` (e.g. pre-compressed public assets) and
empty bodies are passed through untouched.

## Brotli and stream compression

The native [`CompressionStream`](https://developer.mozilla.org/en-US/docs/Web/API/CompressionStream)
Expand Down Expand Up @@ -245,6 +266,7 @@ H3-compression has a concept of composable utilities that accept `event` (from `
- `useBrotliCompression(event, response)`
- `useZstdCompression(event, response)`  – requires Node >= 22.15
- `useCompression(event, response, options?)`  – pass `{ zstd: true }` to include zstd
- `compress(event, response, method?, options?)`  – low-level helper; negotiates when `method` is omitted

#### Stream Compression

Expand All @@ -253,6 +275,7 @@ H3-compression has a concept of composable utilities that accept `event` (from `
- `useBrotliCompressionStream(event, response)`
- `useZstdCompressionStream(event, response)`  – requires Node >= 22.15
- `useCompressionStream(event, response, options?)`  – pass `{ brotli: true }` / `{ zstd: true }`
- `compressStream(event, response, method?, options?)`  – low-level helper; negotiates when `method` is omitted

#### Middleware (h3 v2)

Expand Down
9 changes: 4 additions & 5 deletions src/compression.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { H3Event } from 'h3'
import type { CompressionOptions, RenderResponse } from './helper'
import { compress, getAnyCompression } from './helper'
import { compress } from './helper'

/**
* Compresses the response with [zlib.gzip]{@link https://www.w3schools.com/nodejs/ref_zlib.asp}
Expand Down Expand Up @@ -57,7 +57,8 @@ export async function useZstdCompression(

/**
* Compresses the response with [Zlib]{@link https://www.w3schools.com/nodejs/ref_zlib.asp}
* by 'Accept-Encoding' header. Best is used first.
* by 'Accept-Encoding' header. The client's quality values decide; among
* equally weighted codings the best is used first. Sets `Vary: Accept-Encoding`.
*
* Zstd is only picked when enabled via `options.zstd` and supported by the runtime.
* @param { H3Event } event - A H3 event object.
Expand All @@ -70,7 +71,5 @@ export async function useCompression(
response: Partial<RenderResponse>,
options: CompressionOptions = {},
): Promise<void> {
const compression = getAnyCompression(event, options)
if (compression)
await compress(event, response, compression, options)
await compress(event, response, undefined, options)
}
10 changes: 4 additions & 6 deletions src/compressionStream.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { H3Event } from 'h3'
import type { RenderResponse, StreamCompressionOptions } from './helper'
import { compressStream, getStreamCompression } from './helper'
import { compressStream } from './helper'

/**
* Compresses the response with
Expand Down Expand Up @@ -60,7 +60,8 @@ export async function useZstdCompressionStream(
/**
* Compresses the response with
* [CompressionStream]{@link https://developer.mozilla.org/en-US/docs/Web/API/CompressionStream}
* by 'Accept-Encoding' header. Best is used first.
* by 'Accept-Encoding' header. The client's quality values decide; among
* equally weighted codings the best is used first. Sets `Vary: Accept-Encoding`.
*
* Zstd and brotli are only picked when enabled via `options`.
* @param event - A H3 event object.
Expand All @@ -72,8 +73,5 @@ export async function useCompressionStream(
response: Partial<RenderResponse>,
options: StreamCompressionOptions = {},
) {
const compression = getStreamCompression(event, options)

if (compression)
await compressStream(event, response, compression)
await compressStream(event, response, undefined, options)
}
Loading
Loading