Response Compression
Compress HTTP responses with gzip, deflate, or brotli to reduce bandwidth usage and improve application performance.
On this page
Compression That Just Works
Enable compression with one line.
gzip, deflate, or brotli. Automatic detection. 60-80% bandwidth reduction.
It's This Simple
Enable compression with one line
typescript
Why Compression Matters
Without compression, you're wasting bandwidth, slowing down page loads, and increasing server costs. With MoroJS, you get 60-80% bandwidth reduction automatically.
Traditional compression setup requires manual configuration. We handle that automatically.
Without Compression
- •Wasted bandwidth (uncompressed responses)
- •Slower page loads
- •Higher server costs
- •Poor mobile experience
With MoroJS
- •60-80% bandwidth reduction
- •Faster page loads
- •Lower server costs
- •Better mobile experience
Why It Makes Sense
Automatic
Detects client support and compresses automatically
Efficient
60-80% bandwidth reduction with minimal CPU overhead
Flexible
gzip, deflate, or brotli. Configurable per route.
How It Works
MoroJS provides built-in response compression middleware that automatically compresses responses using gzip, deflate, or brotli based on client support. The middleware detects the client's Accept-Encoding header and compresses responses larger than a configurable threshold.
Basic Compression
MoroJS provides built-in response compression middleware that automatically compresses responses using gzip, deflate, or brotli based on client support.
Basic Compression Setup
typescript
Default Behavior
When compression is enabled, MoroJS automatically:
- • Detects client compression support via Accept-Encoding header
- • Compresses responses larger than 1KB by default
- • Uses gzip for maximum compatibility
- • Falls back to deflate if gzip is not supported
- • Skips compression for already-compressed content
Advanced Configuration
Custom Compression Settings
typescript
Compression Levels
- •
0-3- Fast compression, larger files - •
4-6- Balanced (default: 6) - •
7-9- Maximum compression, slower
Compression Formats
- •
gzip- Widest browser support - •
deflate- Fallback option - •
brotli- Best compression (modern browsers)
Per-Route Compression
Route-Specific Compression
typescript
Best Practices
Do
- • Enable compression for text-based content (JSON, HTML, CSS, JS)
- • Use level 6 for balanced performance
- • Set appropriate thresholds (1KB+) to avoid overhead
- • Enable brotli for modern browsers
- • Compress API responses with large payloads
Don't
- • Compress already-compressed formats (images, videos)
- • Use maximum compression for real-time endpoints
- • Compress very small responses (< 1KB)
- • Compress streaming responses without careful configuration
- • Ignore client compression preferences
Production Configuration
typescript
Performance Impact
Compression Benefits
- • Bandwidth reduction: 60-80% for text-based content
- • Faster page loads: Reduced transfer time
- • Lower server costs: Less bandwidth usage
- • Better mobile experience: Faster on slow connections
- • CPU overhead: Minimal (~5-10ms per response)