From 73ef6091408f43ecc1cc6cfe83c837344626c670 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Fri, 1 Sep 2023 03:45:39 +0100 Subject: [PATCH] Append hash to CSS assets for cache busting --- Cargo.lock | 13 +++++++++++++ Cargo.toml | 2 ++ src/main.rs | 42 +++++++++++++++++++++++++++++++++++------- templates/base.html | 2 +- templates/repo/about.html | 4 ++-- templates/repo/commit.html | 4 ++-- templates/repo/diff.html | 4 ++-- templates/repo/file.html | 4 ++-- 8 files changed, 59 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 565a31a..e95a417 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1855,10 +1855,12 @@ dependencies = [ "md5", "moka", "nom", + "once_cell", "parking_lot 0.12.1", "path-clean", "rsass", "serde", + "sha2", "sled", "syntect", "time", @@ -2063,6 +2065,17 @@ dependencies = [ ] [[package]] +name = "sha2" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479fb9d862239e610720565ca91403019f2f00410f1864c5aa7479b950a76ed8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] name = "sharded-slab" version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/Cargo.toml b/Cargo.toml index f1b8b62..5f0b18f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,9 +25,11 @@ humantime = "2.1" nom = "7.1" md5 = "0.7" moka = { version = "0.11.1", features = ["future"] } +once_cell = "1.18" path-clean = "1.0.1" parking_lot = "0.12" serde = { version = "1.0", features = ["derive"] } +sha2 = "0.10" syntect = "5" sled = { version = "0.34", features = ["compression"] } time = { version = "0.3", features = ["serde"] } diff --git a/src/main.rs b/src/main.rs index cb49875..0ed6f08 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,9 @@ use axum::{ }; use bat::assets::HighlightingAssets; use clap::Parser; +use once_cell::sync::{Lazy, OnceCell}; +use sha2::digest::FixedOutput; +use sha2::Digest; use sled::Db; use syntect::html::ClassStyle; use tokio::{ @@ -40,6 +43,12 @@ mod syntax_highlight; const CRATE_VERSION: &str = clap::crate_version!(); +static GLOBAL_CSS: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/statics/css/style.css",)); +static GLOBAL_CSS_HASH: Lazy> = Lazy::new(|| build_asset_hash(GLOBAL_CSS)); + +static HIGHLIGHT_CSS_HASH: OnceCell> = OnceCell::new(); +static DARK_HIGHLIGHT_CSS_HASH: OnceCell> = OnceCell::new(); + #[derive(Parser, Debug)] #[clap(author, version, about)] pub struct Args { @@ -114,6 +123,7 @@ async fn main() -> Result<(), Box> { .into_boxed_str() .into_boxed_bytes(), ); + HIGHLIGHT_CSS_HASH.set(build_asset_hash(css)).unwrap(); let dark_theme = bat_assets.get_theme("TwoDark"); let dark_css = @@ -123,6 +133,9 @@ async fn main() -> Result<(), Box> { .into_boxed_str() .into_boxed_bytes(), ); + DARK_HIGHLIGHT_CSS_HASH + .set(build_asset_hash(dark_css)) + .unwrap(); let static_favicon = |content: &'static [u8]| { move || async move { @@ -149,14 +162,20 @@ async fn main() -> Result<(), Box> { let app = Router::new() .route("/", get(methods::index::handle)) .route( - "/style.css", - get(static_css(include_bytes!(concat!( - env!("OUT_DIR"), - "/statics/css/style.css" - )))), + &format!("/style-{}.css", *GLOBAL_CSS_HASH), + get(static_css(GLOBAL_CSS)), + ) + .route( + &format!("/highlight-{}.css", HIGHLIGHT_CSS_HASH.get().unwrap()), + get(static_css(css)), + ) + .route( + &format!( + "/highlight-dark-{}.css", + DARK_HIGHLIGHT_CSS_HASH.get().unwrap() + ), + get(static_css(dark_css)), ) - .route("/highlight.css", get(static_css(css))) - .route("/highlight-dark.css", get(static_css(dark_css))) .route( "/favicon.ico", get(static_favicon(include_bytes!("../statics/favicon.ico"))), @@ -223,6 +242,15 @@ async fn run_indexer( .await } +#[must_use] +pub fn build_asset_hash(v: &[u8]) -> Box { + let mut hasher = sha2::Sha256::default(); + hasher.update(v); + let mut out = hex::encode(hasher.finalize_fixed()); + out.truncate(10); + Box::from(out) +} + #[instrument(skip(t))] pub fn into_response(t: &T) -> Response { match t.render() { diff --git a/templates/base.html b/templates/base.html index 4c02da1..3000365 100644 --- a/templates/base.html +++ b/templates/base.html @@ -4,7 +4,7 @@ {% block title %}rgit{% endblock %} - + {%- block head -%}{%- endblock %} diff --git a/templates/repo/about.html b/templates/repo/about.html index c0e8565..d111304 100644 --- a/templates/repo/about.html +++ b/templates/repo/about.html @@ -3,8 +3,8 @@ {% block head -%} {%- if let Some(readme) = readme -%} {%- if readme.0 == crate::git::ReadmeFormat::Markdown %} - - + + {%- endif -%} {%- endif -%} {% endblock %} diff --git a/templates/repo/commit.html b/templates/repo/commit.html index 15097ea..caa78f0 100644 --- a/templates/repo/commit.html +++ b/templates/repo/commit.html @@ -1,8 +1,8 @@ {% extends "repo/base.html" %} {% block head %} - - + + {%- endblock %} {% block commit_nav_class %}active{% endblock %} diff --git a/templates/repo/diff.html b/templates/repo/diff.html index 42fd90d..dba30f1 100644 --- a/templates/repo/diff.html +++ b/templates/repo/diff.html @@ -1,8 +1,8 @@ {% extends "repo/base.html" %} {%- block head %} - - + + {%- endblock -%} {% block diff_nav_class %}active{% endblock %} diff --git a/templates/repo/file.html b/templates/repo/file.html index d2727ec..9e09908 100644 --- a/templates/repo/file.html +++ b/templates/repo/file.html @@ -1,8 +1,8 @@ {% extends "repo/base.html" %} {% block head %} - - + + {%- endblock %} {% block tree_nav_class %}active{% endblock %} -- libgit2 1.7.2