Skip to content

Compression

@modularityjs/http-fastify-compression is a thin wrapper around @fastify/compress that compresses HTTP responses based on the client's Accept-Encoding header. Brotli (br) is preferred when offered, then gzip, then deflate.

Setup

ts
import { HttpModule } from '@modularityjs/http';
import { HttpFastifyModule } from '@modularityjs/http-fastify';
import { HttpFastifyCompressionModule } from '@modularityjs/http-fastify-compression';

const modules = [
  HttpModule.forRoot({ port: 3000 }),
  HttpFastifyModule,
  HttpFastifyCompressionModule, // defaults work for most apps
];

Configuration

OptionDefaultNotes
encodings['br', 'gzip', 'deflate']Encodings the server is willing to use, in preference order. The first that the client also accepts wins.
threshold1024Skip compressing responses smaller than this many bytes — gzip overhead can make tiny responses larger.
removeContentLengthHeadertrueDrop the upstream Content-Length header on compressed responses (it no longer matches the wire body).
globaltrueApply compression globally. Set to false and call reply.compress(payload) from a handler for fine-grained use.

HttpFastifyCompressionModule.forRoot({ encodings: ['gzip'] }) to limit to a single encoding (e.g. behind a reverse proxy that handles brotli itself).

Ordering note

Internally the module registers @fastify/compress during afterLoad, before HttpFastifyModule registers controller routes in onInit. This ordering is required by @fastify/compress — it installs an onSend hook that only applies to routes registered after the plugin.