diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 41a5c60..1097837 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -44,18 +44,27 @@ pub fn app(pool: SqlitePool) -> Router { ) }), ) - .route( - "/static/app.js", - get(|| asset("text/javascript", include_str!("../../static/app.js"))), - ) - .route( - "/static/app.css", - get(|| asset("text/css", include_str!("../../static/app.css"))), - ) + .route("/static/app.js", get(|| asset("text/javascript", APP_JS))) + .route("/static/app.css", get(|| asset("text/css", APP_CSS))) .layer(TraceLayer::new_for_http()) .with_state(pool) } +const APP_CSS: &str = include_str!("../../static/app.css"); +const APP_JS: &str = include_str!("../../static/app.js"); + +/// Hash of app.css + app.js, appended as `?v=` to their URLs so a deploy +/// bypasses the browser cache. +pub fn asset_version() -> &'static str { + static VERSION: std::sync::LazyLock = std::sync::LazyLock::new(|| { + use std::hash::{Hash, Hasher}; + let mut h = std::collections::hash_map::DefaultHasher::new(); + (APP_CSS, APP_JS).hash(&mut h); + format!("{:x}", h.finish()) + }); + &VERSION +} + // En livereload, le CSS/JS doit être relu à chaque rechargement. const CACHE_CONTROL: &str = if cfg!(feature = "livereload") { "no-cache" diff --git a/templates/base.html b/templates/base.html index 80471d7..796db32 100644 --- a/templates/base.html +++ b/templates/base.html @@ -5,11 +5,11 @@ {% block title %}Tempus{% endblock %} · Tempus - + - +