original logo and caching implemented
This commit is contained in:
@@ -65,6 +65,10 @@ impl Hooks for App {
|
||||
async fn after_routes(router: axum::Router, ctx: &AppContext) -> Result<axum::Router> {
|
||||
let casbin = crate::shared::rbac::layer().await?;
|
||||
Ok(router
|
||||
// Stamp a long-lived Cache-Control on /static/* responses only.
|
||||
.layer(axum::middleware::from_fn(
|
||||
crate::shared::cache::static_cache,
|
||||
))
|
||||
.layer(casbin)
|
||||
.layer(axum::middleware::from_fn_with_state(
|
||||
ctx.clone(),
|
||||
|
||||
32
src/shared/cache.rs
Normal file
32
src/shared/cache.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
//! Long-lived `Cache-Control` for static assets.
|
||||
//!
|
||||
//! Loco's static-file middleware serves `/static/*` without any caching headers,
|
||||
//! so browsers refetch the CSS/JS/images on every visit (Lighthouse: "Use
|
||||
//! efficient cache lifetimes"). This middleware stamps a one-year immutable
|
||||
//! cache on those responses only — dynamic HTML pages are left untouched so
|
||||
//! carts, CSRF tokens and prices stay fresh.
|
||||
//!
|
||||
//! Safe because the cache-busting lives in the URL: `app.css?v=…` is query-
|
||||
//! versioned and the vendored libs (`htmx-1.9.12`, `alpinejs-3.14.9`) carry
|
||||
//! their version in the filename. The handful of unversioned images under
|
||||
//! `/static/img` (logo, store hero) must be renamed (or query-busted) to force
|
||||
//! a refresh.
|
||||
|
||||
use axum::{
|
||||
extract::Request,
|
||||
http::header::{HeaderValue, CACHE_CONTROL},
|
||||
middleware::Next,
|
||||
response::Response,
|
||||
};
|
||||
|
||||
const ONE_YEAR_IMMUTABLE: HeaderValue =
|
||||
HeaderValue::from_static("public, max-age=31536000, immutable");
|
||||
|
||||
pub async fn static_cache(req: Request, next: Next) -> Response {
|
||||
let is_static = req.uri().path().starts_with("/static/");
|
||||
let mut res = next.run(req).await;
|
||||
if is_static {
|
||||
res.headers_mut().insert(CACHE_CONTROL, ONE_YEAR_IMMUTABLE);
|
||||
}
|
||||
res
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
//! Cross-cutting helpers used across feature slices.
|
||||
|
||||
pub mod cache;
|
||||
pub mod csrf;
|
||||
pub mod currency;
|
||||
pub mod guard;
|
||||
|
||||
Reference in New Issue
Block a user