Implement a multi-algorithm strategy (gzip and brotli)
masterFor maximum compatibility and performance, provide both gzip and brotliCompress.
- Brotli provides 15-20% better compression than gzip and is ideal for modern browsers.
- Gzip serves as a fallback for older browsers.
When both are provided, the server can automatically serve the best format based on the client's Accept-Encoding header.
compression({
algorithms: [
defineAlgorithm('gzip', { level: 9 }), // Wide browser support
defineAlgorithm('brotliCompress', { // Better compression
params: {
[require('zlib').constants.BROTLI_PARAM_QUALITY]: 11
}
})
]
})