@fastify/compress plugin to automatically compress HTTP responses. It supports various compression algorithms including gzip, brotli (br), and deflate.
Why is Compression Needed?
Compression reduces the size of data transmitted over the network.Without Compression
With Compression
Benefits:- Transfer size: 100KB to 10KB (90% reduction)
- Download time: 1 second to 0.1 second (10x faster)
- Bandwidth savings
Basic Configuration
sonamu.config.ts
- threshold: 1024 (only compress 1KB or larger)
- encodings:
["br", "gzip", "deflate"](in priority order) - Automatically applied to all APIs
Configuration Options
- boolean
- CompressOptions
Simple enable/disable
Compression Algorithms
Brotli (br)
The algorithm with the highest compression ratio- Compression ratio: Best (15-20% better than gzip)
- Speed: Slow (high CPU usage)
- Support: Modern browsers (not supported in IE)
- Static content (can be pre-compressed)
- When bandwidth is critical
- When server CPU has capacity
Gzip
The most universal algorithm- Compression ratio: Medium
- Speed: Fast
- Support: All browsers
- Dynamic API responses
- Real-time compression
- When compatibility is important
Deflate
Legacy algorithm- Compression ratio: Similar to gzip
- Speed: Fast
- Support: Most browsers
Compression Priority
Set priority when the browser supports multiple encodings.Threshold (Minimum Size)
Small responses are not compressed.- Small data has low compression efficiency
- Compression/decompression overhead may be larger
- HTTP header size increases
- Default:
1024(1KB) - Aggressive:
256(256B) - Conservative:
4096(4KB)
Example
Custom Types
Compress only specific Content-Types.text/*(text/html, text/css, text/plain)application/jsonapplication/javascriptapplication/xml
image/png,image/jpeg(already compressed formats)video/mp4(already compressed format)- Additional compression has no effect
Control with Function
Practical Configuration Examples
1. Basic Configuration (Recommended)
- threshold: 1024 (1KB)
- encodings:
["br", "gzip", "deflate"]
2. High-Performance API Server
- Maximum compression ratio (brotli priority)
- Compress everything 256 bytes and above
- Bandwidth optimization
3. Legacy Browser Support
- Support for old browsers like IE
- Use gzip only (stable)
4. Selective Compression
When you want to control compression per API:- Most APIs donβt need compression
- Selectively compress specific APIs
5. CPU Saving (Conservative)
- Minimize CPU usage
- Only compress large responses
- Fast processing
Environment-Based Configuration
- Easier debugging
- Fast response (no compression overhead)
- Bandwidth savings
- Improved user experience
Verifying Compression
Browser Developer Tools
curl Command
Precautions
Performance Impact
Advantages
- Bandwidth savings: 70-90% size reduction
- Download speed: Especially effective on slow networks
- Cost reduction: Reduced CDN transfer volume
Disadvantages
- Increased CPU usage: Compression/decompression overhead
- Response latency: Additional compression time (usually a few ms)
- Memory usage: Compression buffers