Versionne les URL de app.css et app.js
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
b4702fdf71
commit
aa4f5aeb0a
2 files changed
+19
-10
No files matched your search
+17
-8
@@ -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<String> = 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"
|
||||
|
||||
+2
-2
@@ -5,11 +5,11 @@
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>{% block title %}Tempus{% endblock %} · Tempus</title>
|
||||
<link rel="stylesheet" href="/static/flatpickr/flatpickr.min.css">
|
||||
<link rel="stylesheet" href="/static/app.css">
|
||||
<link rel="stylesheet" href="/static/app.css?v={{ crate::routes::asset_version() }}">
|
||||
<script src="/static/htmx.min.js" defer></script>
|
||||
<script src="/static/flatpickr/flatpickr.min.js" defer></script>
|
||||
<script src="/static/flatpickr/fr.min.js" defer></script>
|
||||
<script src="/static/app.js" defer></script>
|
||||
<script src="/static/app.js?v={{ crate::routes::asset_version() }}" defer></script>
|
||||
</head>
|
||||
<body>
|
||||
<header class="topbar">
|
||||
|
||||
Reference in new issue
Block a user