Skip to main content
By adding the compress option to Sonamu’s @api decorator, you can finely control compression on a per-API basis.

Basic Usage

Adding to @api Decorator

Configuration Methods

The simplest method

Priority

Compression settings are applied in the following order:
  1. compress in @api decorator (highest priority)
  2. Global plugins.compress setting
  3. Default (no compression)

Example

Result: aggressive applied (decorator takes priority)

Disabling Compression

To not compress a specific API:
Use cases:
  • Already compressed files (images, videos, zip)
  • Very small responses (< 100 bytes)
  • Real-time streaming
  • Avoiding decompression errors

Selective Compression (global: false)

To not compress by default and selectively compress specific APIs:
Advantages:
  • Prevent unnecessary compression
  • Minimize CPU usage
  • Precise control

Strategy by Response Size

Small Responses (< 1KB)

Reason: Compression overhead is greater than the benefit

Medium Responses (1KB-100KB)

Reason: Good compression effectiveness

Large Responses (> 100KB)

Reason: Network time savings far outweigh compression time

Strategy by Content Type

JSON API

Characteristic: JSON compresses very well (70-90% reduction)

Images/Binary

Reason: Already compressed formats (JPEG, PNG, MP4, etc.)

HTML/CSS

Characteristic: Text-based, very good compression effectiveness

Practical Examples

1. E-commerce API

2. File Download API

3. Statistics/Report API

4. Streaming API

SSR Page Compression

Compression settings are also available for SSR routes.

Conditional Compression

While you cannot control compression at runtime based on conditions, you can separate into multiple APIs:

Compression Option Override

You can change only part of the global settings:

Monitoring

To verify compression effectiveness:
Check in browser developer tools:

Precautions

Precautions for per-API compression control:
  1. Disable for already compressed content: images, videos, zip
  2. Compression is inefficient for small responses: Better not to compress < 1KB
  3. Disable compression for streaming: SSE, WebSocket
  4. Use consistent strategy: Similar APIs should have the same settings
  5. Monitor performance: Check CPU load due to compression

Debugging Tips

1. Verify Compression is Applied

2. Compare Compression Sizes

3. Check Compression Algorithm

Next Steps

Compression Configuration

Global compression plugin settings

Compression Presets

Predefined compression settings

Performance Optimization

Compression optimization strategies