#rocket #serve-static #static #serve #http

nightly rocket-static-files

Serve static files with cache headers with Rocket

1 unstable release

0.1.0 Jan 16, 2021

#384 in Caching

MPL-2.0 license

15KB
257 lines

rocket-static-files

Serves static files with far-future cache headers, and version specific URLs.

Usage

Dependencies

Add the following dependencies:

[dependencies]
rocket-static-files = "0.1"

[build-dependencies]
rocket-static-files = { version = "0.1", features = [ "gen" ] }

Build Script

To generate the hashes, add the following to your build.rs (this assumes your static files are located at $CARGO_MANIFEST_DIR/static):

use std::path::PathBuf;

fn main() {
    let mut static_root =
        PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap());
    static_root.push("static");

    let mut out_path = PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
    out_path.push("static_file_hashes.rs");

    rocket_static_files::generate(&out_path, &static_root).unwrap();
}

Fairing

use rocket_static_files::StaticFiles;

include!(concat!(env!("OUT_DIR"), "/static_file_hashes.rs"));

fn main() {
    rocket::ignite()
        .attach(StaticFiles::fairing(&STATIC_FILE_HASHES)
        .launch();
}

Rocket.toml

Add a section like this:

[global.static_files]
serve_from = "./static"         # Relative to Rocket.toml
path_prefix = "/static"         # Where to serve the files: http://127.0.0.1:8000/static

Dependencies

~9–17MB
~243K SLoC