Skip to content

Commit 0b8924d

Browse files
committed
Document dynamic middleware options, rework docs
1 parent d13facf commit 0b8924d

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

packages/fetch-router/README.md

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -874,17 +874,25 @@ let router = createRouter({
874874

875875
The middleware is a thin wrapper around the `compress()` response helper that accepts the same options.
876876

877-
The middleware applies an additional **Content-Type filter** to only apply compression to appropriate media types (MIME types). By default, this uses the `isCompressibleMediaType(mediaType)` helper to check if the media type is compressible. You can customize this behavior with the `filterMediaType` option, re-using the built-in filter if needed.
877+
Since this middleware is designed to handle multiple response types at once, the `encodings`, `zlib` and `brotli` options can also be functions that have access to the response before providing a value. For example, to use different encodings for different media types:
878878

879-
#### `isCompressibleMediaType(mediaType)`
880-
881-
The `isCompressibleMediaType` helper determines whether a media type should be compressed. It returns `true` for:
879+
```ts
880+
import { compression } from '@remix-run/fetch-router/compression-middleware'
882881

883-
- Known compressible types from the [mime-db](https://www.npmjs.com/package/mime-db) database (e.g., `application/json`, `text/html`, `text/css`), except those starting with `x-` (experimental) or `vnd.` (vendor-specific).
884-
- All `text/*` types (e.g., `text/plain`, `text/markdown`)
885-
- Types with structured data suffixes: `+json`, `+text`, or `+xml` (e.g., `application/vnd.api+json`, `image/svg+xml`)
882+
let router = createRouter({
883+
middleware: [
884+
compression({
885+
encodings: (response) => {
886+
return response.headers.get('Content-Type')?.split(';')[0].trim() === 'text/event-stream'
887+
? ['gzip', 'deflate']
888+
: ['br', 'gzip', 'deflate']
889+
},
890+
}),
891+
],
892+
})
893+
```
886894

887-
This helper is primarily intended for use in custom `filterMediaType` functions for the `compression()` middleware:
895+
The middleware also applies an additional **Content-Type filter** to only apply compression to appropriate media types (MIME types). By default, this uses the `isCompressibleMediaType(mediaType)` helper to check if the media type is compressible. You can customize this behavior with the `filterMediaType` option, re-using the built-in filter if needed.
888896

889897
```ts
890898
import {
@@ -903,6 +911,14 @@ let router = createRouter({
903911
})
904912
```
905913

914+
The `isCompressibleMediaType` helper determines whether a media type should be compressed. It returns `true` for:
915+
916+
- Known compressible types from the [mime-db](https://www.npmjs.com/package/mime-db) database (e.g., `application/json`, `text/html`, `text/css`), except those starting with `x-` (experimental) or `vnd.` (vendor-specific).
917+
- All `text/*` types (e.g., `text/plain`, `text/markdown`)
918+
- Types with structured data suffixes: `+json`, `+text`, or `+xml` (e.g., `application/vnd.api+json`, `image/svg+xml`)
919+
920+
This helper is primarily intended for use in custom `filterMediaType` functions for the `compression()` middleware.
921+
906922
### Testing
907923

908924
Testing is straightforward because `fetch-router` uses the standard `fetch()` API:

0 commit comments

Comments
 (0)