Pre-rendering Static Sites with build.rs

The trick: do all the hard work at build time. With Rust's build scripts, you can generate your HTML at compile time and embed it directly into the Wasm binary.

The Pattern

// build.rs
pub fn main() {
    let html = render_all_the_things();
    std::fs::write(
        std::path::Path::new(&std::env::var("OUT_DIR").unwrap()).join("generated.rs"),
        format!("const HTML: &str = {:?};", html)
    ).unwrap();
}

At runtime, include the generated file:

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

pub fn handle(req: Request) -> Response {
    Response::builder().body(HTML).unwrap()
}
Zero template engine in your Wasm binary. The smallest possible artifact size with no runtime rendering cost whatsoever.