Fastify compression utils
Clone or download
Latest commit cdd3c09 Nov 20, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore Updated .gitignore Nov 17, 2017
.travis.yml Add node11 Nov 20, 2018
LICENSE Initial commit Nov 8, 2017 added customTypes unit tests and improved doc for that option Aug 28, 2018
index.js Log debug instead of warn for missing payload Nov 20, 2018
package.json 0.7.1 Nov 20, 2018
test.js Added buffer support (#35) Sep 17, 2018


Greenkeeper badge

Build Status js-standard-style

Adds compression utils to the Fastify reply object.
Support gzip, deflate and brotli.


npm i fastify-compress --save


This plugins adds two functionalities to Fastify, a compress utility and a global compression hook.

Currently the following headers are supported:

  • 'deflate'
  • 'gzip'
  • 'br'
  • '*'

If the 'accept-encoding' header specifies no preferred encoding with an asterisk * the payload will be compressed with gzip.

If an unsupported encoding is received, it will automatically return a 406 error, if the 'accept-encoding' header is missing, it will not compress the payload.

It automatically defines if a payload should be compressed or not based on its Content-Type, if no content type is present, it will assume is application/json.

Global hook

The global compression hook is enabled by default if you want to disable it, pass the option { global: false }.

  { global: false }

Remember that thanks to the Fastify encapsulation model, you can set a global compression, but running it only in a subset of routes is you wrap them inside a plugin.


This plugin add a compress function to reply that accepts a stream or a string and compress it based on the 'accept-encoding' header. If a js object is passed in, will be stringified as json.

const fs = require('fs')
const fastify = require('fastify')

fastify.register(require('fastify-compress'), { global: false })

fastify.get('/', (req, reply) => {

fastify.listen(3000, function (err) {
  if (err) throw err
  console.log(`server listening on ${fastify.server.address().port}`)



You can set a custom threshold below which it will not be made compression, default to 1024.

  { threshold: 2048 }


mime-db is used to determine if a Content-Type should be compressed. You can compress additional content types via regular expression.

  { customTypes: /x-protobuf$/ }


Brotli compression is not enabled by default, if you need it we recommend to install iltorb and pass it as option.

  { brotli: require('iltorb') }

Disable compression by header

You can selectively disable the response compression by using the x-no-compression header in the request.


Please have in mind that in large scale scenarios, you should use a proxy like Nginx to handle response-compression.


This project is kindly sponsored by:


Licensed under MIT.