#minify-html #middleware #response #response-body #axum #applications #outcome

axum_html_minifier

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.

1 stable release

1.0.0 Jan 18, 2025

#4 in #outcome

Download history 110/week @ 2025-01-15

110 downloads per month

GPL-3.0 license

26KB
62 lines

Axum HTML minifier Middleware

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.

Dependencies

~18–25MB
~315K SLoC