static-files
Shipping a Rust server with a frontend usually means shipping two things: the binary and a directory of files it must find at runtime. This removes the second one. The files are read at build time, turned into Rust data and compiled in.
What changes for you
- One artefact to deploy. No assets volume, no
--static-dirflag, no "works locally, 404s in production". - No filesystem call per request. The bytes are already in the process, which is also why a cold container has no warm-up for them.
- Your frontend build runs in the same step.
NpmBuildinstalls dependencies, runsnpm run build(or yarn/pnpm) and bundles the result —cargo buildis the whole pipeline. - Fast rebuilds. Change detection is on by default, so touching a file re-bundles, and touching nothing does not.
- Deterministic output. Resource order is sorted by default, with filters and module splitting when a bundle gets big.
Getting it
cargo add static-files --build
cargo add static-filesMinimal case — a folder of files:
use static_files::resource_dir;
fn main() -> std::io::Result<()> {
resource_dir("./static").build()
}include!(concat!(env!("OUT_DIR"), "/generated.rs"));
let resources = generate(); // index.html, app.js, … as bytesWith a frontend that needs building first:
use static_files::NpmBuild;
fn main() -> std::io::Result<()> {
NpmBuild::new("web")
.install()?
.run("build")?
.target("web/dist")
.to_resource_dir()
.build()
}
NpmBuild skips the install and the build when nothing changed, so this
is cheap on a warm tree.