📦 trillium-static-compiled — embedded static file serving
Serves static files embedded directly into the binary at compile time. No runtime feature flag
required. For serving files from the filesystem at runtime, see
trillium-static.
Example
use static_compiled;
let handler = static_compiled!.with_index_file;
// run with your chosen runtime adapter, e.g.:
// trillium_tokio::run(handler);
The static_compiled! macro embeds the directory contents at compile time, so the binary is
self-contained — no filesystem access at runtime.
ETag headers (default-on)
Each file is hashed at compile time and served with an ETag response header. The baked tag is
byte-identical to what
trillium_caching_headers::Etag
would compute at runtime, so adding Etag::new() after this handler in the chain gives you
If-None-Match / 304 Not Modified for free without re-hashing the body.
Opt out per invocation: static_compiled!("./public", etag = false).
Precompression (opt-in via cargo features)
Pre-compress bundle contents into Brotli, Zstd, and Gzip variants at build time. Enable one or more encoders via cargo features:
[]
= { = "*", = ["compression"] }
# or pick individual encoders:
# features = ["brotli", "gzip"]
Then opt in per invocation:
static_compiled!; // every enabled encoder
static_compiled!; // a specific subset
The macro runs each encoder at maximum quality, in parallel via rayon. Per-file variants are sorted
smallest-first and only baked when they save at least 5%; files under 256 bytes are skipped
entirely. At request time the handler picks the smallest variant the client's Accept-Encoding
allows, sets Content-Encoding, and emits Vary: Accept-Encoding.
This composes with trillium-compression, which passes
through any response that already has Content-Encoding set.
Safety
This crate uses #![forbid(unsafe_code)].