Skip to main content

Crate salvo_compression

Crate salvo_compression 

Source
Expand description

Compression middleware for the Salvo web framework.

This middleware automatically compresses HTTP responses using various algorithms, reducing bandwidth usage and improving load times for clients.

§Supported Algorithms

AlgorithmFeatureContent-Encoding
Gzipgzipgzip
Brotlibrotlibr
Deflatedeflatedeflate
Zstdzstdzstd

§Example

use salvo_compression::{Compression, CompressionLevel};
use salvo_core::prelude::*;

let compression = Compression::new()
    .enable_gzip(CompressionLevel::Default)
    .min_length(1024);  // Only compress responses > 1KB

let router = Router::new()
    .hoop(compression)
    .get(my_handler);

§Algorithm Negotiation

The middleware negotiates the compression algorithm based on the client’s Accept-Encoding header. By default, it respects the client’s preference order. Use force_priority(true) to use the server’s configured priority instead.

§Compression Levels

§Default Content Types

By default, the middleware compresses:

  • text/* (HTML, CSS, plain text, etc.)
  • application/javascript
  • application/json
  • application/xml, application/rss+xml
  • application/wasm
  • image/svg+xml

Use .content_types() to customize which MIME types are compressed.

§Minimum Length

Small responses may not benefit from compression. Use .min_length(bytes) to skip compression for responses smaller than the specified size.

Read more: https://salvo.rs

Structs§

Compression
Compression

Enums§

CompressionAlgo
CompressionAlgo
CompressionLevel
Level of compression data should be compressed with.