Rust library to generate static websites
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat(dev): Add cache headings to fingerprinted assets in dev (#53)

* feat(dev): Add cache headings to fingerprinted assets in dev

* chore: changeset

* refactor: unnecessary change

* fix: remove unnecessary clone

authored by

Erika and committed by
GitHub
6922bb34 e194a1e6

+41 -1
+5
.sampo/changesets/noble-count-ilmatar.md
··· 1 + --- 2 + maudit-cli: patch 3 + --- 4 + 5 + Fixes fingerprinted assets reloading unnecessarily in development by introducing immutable cache headers on them
+36 -1
crates/maudit-cli/src/dev/server.rs
··· 5 5 Request, State, 6 6 ws::{Message, WebSocket, WebSocketUpgrade}, 7 7 }, 8 - http::{HeaderValue, StatusCode, header::CONTENT_LENGTH}, 8 + http::{HeaderValue, StatusCode, Uri, header::CONTENT_LENGTH}, 9 9 middleware::{self, Next}, 10 10 response::{IntoResponse, Response}, 11 11 routing::get, ··· 152 152 let router = Router::new() 153 153 .route("/ws", get(ws_handler)) 154 154 .fallback_service(serve_dir) 155 + .layer(middleware::from_fn(add_cache_headers)) 155 156 .layer(middleware::from_fn(move |req, next| { 156 157 add_dev_client_script(req, next, socket_addr, host) 157 158 })) ··· 260 261 } 261 262 262 263 res 264 + } 265 + 266 + async fn add_cache_headers(req: Request, next: Next) -> Response { 267 + let uri = req.uri().clone(); 268 + let mut res = next.run(req).await; 269 + 270 + if let Some(content_type) = res.headers().get(axum::http::header::CONTENT_TYPE) { 271 + let cache_header = cache_header_by_content(&uri, content_type); 272 + if let Some(cache_header) = cache_header { 273 + res.headers_mut() 274 + .insert(header::CACHE_CONTROL, cache_header); 275 + } 276 + } 277 + 278 + res 279 + } 280 + 281 + fn cache_header_by_content(uri: &Uri, content_type: &HeaderValue) -> Option<HeaderValue> { 282 + if content_type == HeaderValue::from_static("text/html") { 283 + // No cache for HTML files 284 + Some(HeaderValue::from_static( 285 + "no-cache, no-store, must-revalidate", 286 + )) 287 + } 288 + // If something comes from the assets path, assume that it's fingerprinted and can be cached for a long time 289 + // TODO: Same as dist, shouldn't be hardcoded 290 + else if uri.path().starts_with("/_maudit/") { 291 + Some(HeaderValue::from_static( 292 + "public, max-age=31536000, immutable", 293 + )) 294 + } else { 295 + // Don't try to cache anything else, the browser will decide based on the last-modified header 296 + None 297 + } 263 298 } 264 299 265 300 async fn ws_handler(