tower-livereload

Crates.iotower-livereload
lib.rstower-livereload
version
sourcesrc
created_at2022-08-25 20:06:58.842328
updated_at2024-11-29 19:12:49.640392
descriptionA LiveReload middleware built on top of tower.
homepagehttps://github.com/leotaku/tower-livereload
repositoryhttps://github.com/leotaku/tower-livereload
max_upload_size
id652379
Cargo.toml error:TOML parse error at line 19, column 1 | 19 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Leo Gaskin (leotaku)

documentation

README

tower-livereload

Build Status Crates.io Lib.rs Documentation

A middleware for browser reloading, built on top of tower.

Example

Note that axum is only used as an example here, pretty much any Rust HTTP library or framework will be compatible!

use axum::{response::Html, routing::get, Router};
use tower_livereload::LiveReloadLayer;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let app = Router::new()
        .route("/", get(|| async { Html("<h1>Wow, such webdev</h1>") }))
        .layer(LiveReloadLayer::new());

    let listener = tokio::net::TcpListener::bind("0.0.0.0:3030").await?;
    axum::serve(listener, app).await?;

    Ok(())
}

If you continuously rebuild and rerun this example e.g. using watchexec, you should see your browser reload whenever the code is changed.

More examples can be found on GitHub under examples.

Manual reload

With the Reloader utility, it is possible to reload your web browser entirely using hooks from Rust code. See this example on GitHub for pointers on how to implement a self-contained live-reloading static server.

Ecosystem compatibility

tower-livereload has been built from the ground up to provide the highest amount of ecosystem compatibility.

The provided middleware uses the http and http_body crates as its HTTP abstractions. That means it is compatible with any library or framework that also uses those crates, such as hyper, axum, tonic, and warp.

Heuristics

To provide LiveReload functionality, we have to inject code into HTML web pages. To determine whether a page is injectable, some header-based heuristics are used. In particular, Content-Type has to start with text/html and Content-Encoding must not be set.

If LiveReload is not working for some of your pages, ensure that these heuristics apply to your responses. In particular, if you use middleware to compress your HTML, ensure that the LiveReload middleware is applied before your compression middleware.

License

tower-livereload is free and open source software distributed under the terms of either the MIT or the Apache 2.0 license, at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Commit count: 233

cargo fmt