#rocket #gzip #brotli


Response compression in both gzip and brotli formats for the Rocket webserver using the async-compression library

8 releases (4 breaking)

0.5.1 Nov 19, 2023
0.5.0 Jun 15, 2023
0.4.0 Jun 14, 2023
0.3.0 Apr 12, 2023
0.1.0 Nov 2, 2021

#105 in Compression

Download history 36/week @ 2023-08-12 23/week @ 2023-08-19 59/week @ 2023-08-26 63/week @ 2023-09-02 65/week @ 2023-09-09 68/week @ 2023-09-16 46/week @ 2023-09-23 47/week @ 2023-09-30 105/week @ 2023-10-07 143/week @ 2023-10-14 161/week @ 2023-10-21 107/week @ 2023-10-28 74/week @ 2023-11-04 136/week @ 2023-11-11 106/week @ 2023-11-18 180/week @ 2023-11-25

507 downloads per month

Custom license

380 lines

Rocket Async Compression

This library provides response compression in both gzip and brotli formats for the Rocket using the async-compression library.

It currently supports usage with Rocket 0.5.0-rc.2. If you want to use a different version, you'll have to fork this library and change Cargo.toml.

I'd love to get this merged into Rocket itself eventually since I think it would be a very useful addition that I myself can barely live without in a webserver.


Add this to Cargo.toml:

rocket = "0.5.0-rc.2"
rocket_async_compression = "0.5.0"


The following example will enable compression only when the crate is built in release mode. Compression can be very slow when using unoptimized debug builds while developing locally.

extern crate rocket;

use rocket_async_compression::Compression;

async fn rocket() -> _ {
    let server = rocket::build()
        .mount("/", routes![...]);

    if cfg!(debug_assertions) {
    } else {

Cached Compression

When serving static files, it can be useful to avoid the work of compressing the same files repeatedly for each request. This crate provides an alternative CachedCompression fairing which stores cached responses in memory and uses those when available.

Note that cached responses do not expire and will be held in memory for the life of the program. You should only use this fairing for compressing static files that will not change while the server is running.

extern crate rocket;

use rocket::fs::{relative, FileServer};
use rocket_async_compression::CachedCompression;

async fn rocket() -> _ {
        .attach(CachedCompression::path_suffix_fairing(vec![".js", ".css", ".html", ".wasm"]))


~1M SLoC