simplify static assets

This commit is contained in:
2026-08-08 22:41:50 -04:00
parent de46f150f8
commit 20c2afb5ad
+27 -16
View File
@@ -48,12 +48,23 @@ fn content_hash(data: &[u8]) -> String {
hex::encode(hasher.finalize()) hex::encode(hasher.finalize())
} }
/// Constructs a `StaticAssetStore` from `name => path` pairs, embedding each /// Returns the file name portion of a path, e.g. `"../static/style.css"`
/// file's bytes at compile time via `include_bytes!`. /// becomes `"style.css"`. Used to derive an asset's registry key from its
/// path.
fn file_name(path: &'static str) -> &'static str {
match path.rfind('/') {
Some(i) => &path[i + 1..],
None => path,
}
}
/// Constructs a `StaticAssetStore` from paths, embedding each file's bytes at
/// compile time via `include_bytes!`. Each asset is keyed by its file name
/// (derived from the path), so there's no need to repeat the name.
macro_rules! static_assets { macro_rules! static_assets {
($($name:literal => $path:literal),* $(,)?) => { ($($path:literal),* $(,)?) => {
StaticAssetStore::new(&[ StaticAssetStore::new(&[
$(($name, include_bytes!($path))),* $((file_name($path), include_bytes!($path))),*
]) ])
}; };
} }
@@ -61,18 +72,18 @@ macro_rules! static_assets {
/// The app's static assets, loaded once on first use. /// The app's static assets, loaded once on first use.
pub static STORE: LazyLock<StaticAssetStore> = LazyLock::new(|| { pub static STORE: LazyLock<StaticAssetStore> = LazyLock::new(|| {
static_assets! { static_assets! {
"style.css" => "../static/style.css", "../static/style.css",
"passkey-login.js" => "../static/passkey-login.js", "../static/passkey-login.js",
"passkey-register.js" => "../static/passkey-register.js", "../static/passkey-register.js",
"password-toggle.js" => "../static/password-toggle.js", "../static/password-toggle.js",
"rewards.js" => "../static/rewards.js", "../static/rewards.js",
"htmx.min.js" => "../static/htmx.min.js", "../static/htmx.min.js",
"idiomorph-ext.min.js" => "../static/idiomorph-ext.min.js", "../static/idiomorph-ext.min.js",
"htmx-ws.min.js" => "../static/htmx-ws.min.js", "../static/htmx-ws.min.js",
"favicon.ico" => "../static/favicon.ico", "../static/favicon.ico",
"favicon-32x32.png" => "../static/favicon-32x32.png", "../static/favicon-32x32.png",
"apple-touch-icon.png" => "../static/apple-touch-icon.png", "../static/apple-touch-icon.png",
"logo.svg" => "../static/logo.svg", "../static/logo.svg",
} }
}); });