···11+---
22+maudit-cli: patch
33+---
44+55+Fixes fingerprinted assets reloading unnecessarily in development by introducing immutable cache headers on them
+36-1
crates/maudit-cli/src/dev/server.rs
···55 Request, State,
66 ws::{Message, WebSocket, WebSocketUpgrade},
77 },
88- http::{HeaderValue, StatusCode, header::CONTENT_LENGTH},
88+ http::{HeaderValue, StatusCode, Uri, header::CONTENT_LENGTH},
99 middleware::{self, Next},
1010 response::{IntoResponse, Response},
1111 routing::get,
···152152 let router = Router::new()
153153 .route("/ws", get(ws_handler))
154154 .fallback_service(serve_dir)
155155+ .layer(middleware::from_fn(add_cache_headers))
155156 .layer(middleware::from_fn(move |req, next| {
156157 add_dev_client_script(req, next, socket_addr, host)
157158 }))
···260261 }
261262262263 res
264264+}
265265+266266+async fn add_cache_headers(req: Request, next: Next) -> Response {
267267+ let uri = req.uri().clone();
268268+ let mut res = next.run(req).await;
269269+270270+ if let Some(content_type) = res.headers().get(axum::http::header::CONTENT_TYPE) {
271271+ let cache_header = cache_header_by_content(&uri, content_type);
272272+ if let Some(cache_header) = cache_header {
273273+ res.headers_mut()
274274+ .insert(header::CACHE_CONTROL, cache_header);
275275+ }
276276+ }
277277+278278+ res
279279+}
280280+281281+fn cache_header_by_content(uri: &Uri, content_type: &HeaderValue) -> Option<HeaderValue> {
282282+ if content_type == HeaderValue::from_static("text/html") {
283283+ // No cache for HTML files
284284+ Some(HeaderValue::from_static(
285285+ "no-cache, no-store, must-revalidate",
286286+ ))
287287+ }
288288+ // If something comes from the assets path, assume that it's fingerprinted and can be cached for a long time
289289+ // TODO: Same as dist, shouldn't be hardcoded
290290+ else if uri.path().starts_with("/_maudit/") {
291291+ Some(HeaderValue::from_static(
292292+ "public, max-age=31536000, immutable",
293293+ ))
294294+ } else {
295295+ // Don't try to cache anything else, the browser will decide based on the last-modified header
296296+ None
297297+ }
263298}
264299265300async fn ws_handler(