From aa4f5aeb0acec114c3cb46023dd001c624bbad77 Mon Sep 17 00:00:00 2001 From: Romulus21 Date: Fri, 13 Nov 2026 18:39:05 +0100 Subject: [PATCH] Versionne les URL de app.css et app.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Un hash du contenu est ajouté en ?v= : après un déploiement, le navigateur recharge le CSS/JS au lieu de garder l'ancien en cache (max-age=3600). Co-Authored-By: Claude Opus 5.5 --- src/routes/mod.rs | 25 +++++++++++++++++-------- templates/base.html | 4 ++-- 2 files changed, 19 insertions(+), 10 deletions(-) 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 - + - +