original logo and caching implemented

This commit is contained in:
Priec
2026-06-29 12:26:01 +02:00
parent a95a220a97
commit ed3150136a
5 changed files with 37 additions and 0 deletions

BIN
assets/static/logo/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.9 KiB

View File

@@ -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
View 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
}

View File

@@ -1,5 +1,6 @@
//! Cross-cutting helpers used across feature slices.
pub mod cache;
pub mod csrf;
pub mod currency;
pub mod guard;