gzip & brotli Out of the Box
@fastify/compress
Compress responses on the fly with gzip, brotli, or deflate, negotiated through the client's Accept-Encoding header.
What you'll learn
- Install @fastify/compress
- Register with default settings
- Tune the threshold to skip compressing small payloads
Most JSON APIs benefit from compression — a 50 KB response often shrinks to 10 KB. @fastify/compress negotiates the algorithm with the client and pipes the response through a transform stream.
Install & Register
npm install @fastify/compress import compress from '@fastify/compress'
await app.register(compress, {
threshold: 1024,
encodings: ['br', 'gzip', 'deflate'],
}) The encodings order is the preference order — brotli first, gzip second. threshold: 1024 skips compression for payloads under 1 KB, where the overhead is greater than the saving.
Streamed Responses
Compression works on streams too. Wrap a file or generator response and the plugin compresses chunk-by-chunk.
import { createReadStream } from 'node:fs'
app.get('/big.csv', async (req, reply) => {
reply.type('text/csv')
return createReadStream('./reports/big.csv')
}) Skipping Routes
For already-compressed assets (images, video, fingerprinted JS served by a CDN) you don’t want a second pass. Skip per-route:
app.get('/raw', { compress: false }, async () => {
return { tiny: 'payload' }
}) The plugin also auto-skips common binary types like image/png and video/mp4.