//! 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
}
