Skip to main content
Sonamu can automatically compress HTTP responses using @fastify/compress. It supports various compression algorithms like gzip, deflate, and brotli, and you can control compression per API.

Basic Structure

compress Settings

Enable/Disable

Type: boolean | FastifyCompressOptions
Disable:

Key Options

global

Determines whether to automatically apply compression to all responses. Type: boolean Default: true
  • true: Auto-compress all responses (default)
  • false: Control per API with the @api({ compress }) option
We recommend global: false. Small responses or already compressed files (images, videos) don’t need compression.

threshold

Minimum response size to start compression. Type: number (bytes) Default: 1024 (1KB)
Recommended values:
  • 1024 (1KB) - General web apps
  • 512 (512B) - Compress small JSON responses too
  • 5120 (5KB) - Compress only large responses
Compressing very small responses may have more overhead than benefit.

encodings

List of compression algorithms to support. Type: string[] Default: ["gzip", "deflate"]
Supported algorithms:
  • "gzip" - Most widely supported, fast compression
  • "deflate" - Similar to gzip
  • "br" (brotli) - High compression ratio, slower compression
Priority: Uses the first algorithm in the array that the client supports
Brotli has a higher compression ratio but uses more CPU. We recommend pre-compressing static files and using gzip for dynamic responses.

Basic Examples

Including Brotli

Auto Compression (All Responses)

Per-API Compression Control

If you set global: false, you can control compression per API with the compress option of the @api() decorator.
Per-API Compression Control Usage

Practical Examples

Standard Web API

Usage:

CDN Optimization

Development vs Production

High Compression Settings

Increasing Brotli quality improves compression ratio but significantly increases CPU usage. Test before using in production.

Additional Options

gzip Level

gzip levels:
  • Z_BEST_SPEED (1) - Fast compression
  • Z_DEFAULT_COMPRESSION (6) - Default (recommended)
  • Z_BEST_COMPRESSION (9) - Maximum compression

Compress Specific Content-Types Only

Compression Exclusion

Verifying Compression

How to verify if responses are compressed:

Browser Developer Tools

  1. Open Network tab
  2. Check response headers:
  3. Check Size:

curl Test

Performance Impact

Compression Trade-offs

Benefits:
  • Bandwidth savings (50-80% reduction)
  • Faster transfer time (especially on slow networks)
  • CDN cost savings
Drawbacks:
  • Increased CPU usage
  • Increased Time To First Byte (TTFB)
  • Increased memory usage

Recommendations

Should compress:
  • JSON responses (API)
  • HTML, CSS, JavaScript
  • SVG, XML
  • Text files
Should not compress:
  • Images (JPEG, PNG, WebP) - Already compressed
  • Videos (MP4, WebM) - Already compressed
  • Compressed files (ZIP, GZ) - Already compressed
  • Very small responses (< 1KB)

Cautions

1. Already Compressed Files

2. CPU Usage

3. threshold Setting

Next Steps

After completing response compression settings: