axum_html_minifier

Crates.ioaxum_html_minifier
lib.rsaxum_html_minifier
version1.0.2
created_at2025-01-18 15:34:03.986275+00
updated_at2025-03-20 16:34:05.422479+00
descriptionThis crate allows you to includes a custom middleware that minify HTML outcome of a Response in your axum application. The middleware intercepts responses body and minify the entire content. So, we can send fewer packets to the client and increase our throughput.
homepage
repositoryhttps://gitlab.torproject.org/mateolafalce/axum_html_minifier
max_upload_size
id1521864
size82,999
Mateo Lafalce (mateolafalce)

documentation

README

Axum HTML minifier Middleware

crates.io github docs.rs

This crate allows you to includes a custom middleware that minify HTML outcome of a Response in your axum application. The middleware intercepts responses body and minify the entire content. So, we can send fewer packets to the client and increase our throughput.

Usage

cargo add axum_html_minifier

Then add html_minifier in your middleware layer, for example:

use axum::{middleware, Router};

#[tokio::main]
async fn main() {
    let html = std::fs::read("examples/index.html").unwrap();
    let app = Router::new()
        .route(
            "/",
            axum::routing::get(|| async move { axum::response::Html(html) }),
        )
        //.layer(middleware::from_fn(OTHER_MIDDLEWARE_RULE))
        .layer(middleware::from_fn(axum_html_minifier::html_minifier));

    let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
        .await
        .unwrap();
    println!("listening on {}", listener.local_addr().unwrap());
    axum::serve(listener, app).await.unwrap();
}

Example

cargo run --example minify --features DEBUG

When visiting http://127.0.0.1:3000/, the response will show you the comparison between a raw response and a minified response with this crate.

License

This project is licensed under the GPLv3 License. See the LICENSE file for details.

Commit count: 0

cargo fmt