From 4c98cf4b6691cdbe9bcdf77c5340cf8c4c5a565c Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Fri, 5 Aug 2022 11:00:41 +0200 Subject: [PATCH] do not cache html, cache everything else for a long time --- backend/src/static_assets.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/backend/src/static_assets.rs b/backend/src/static_assets.rs index 7fcbddb67b..81d0f62963 100644 --- a/backend/src/static_assets.rs +++ b/backend/src/static_assets.rs @@ -12,6 +12,7 @@ use axum::{ response::IntoResponse, }; +use mime_guess::mime; use rust_embed::RustEmbed; // static_handler is a handler that serves static files from the @@ -46,11 +47,17 @@ fn serve_path(path: String) -> Response { Some(content) => { let body = body::boxed(body::Full::from(content.data)); let mime = mime_guess::from_path(path).first_or_octet_stream(); - Response::builder() - .header(header::CONTENT_TYPE, mime.as_ref()) - .header(header::CACHE_CONTROL, "max-age=3600".to_owned()) - .body(body) - .unwrap() + let mut res = Response::builder().header(header::CONTENT_TYPE, mime.as_ref()); + if mime.as_ref() == mime::APPLICATION_JAVASCRIPT { + res = res.header(header::CACHE_CONTROL, "max-age=31536000"); + } else if (mime.type_(), mime.subtype()) == (mime::TEXT, mime::CSS) { + res = res.header(header::CACHE_CONTROL, "max-age=31536000"); + } else if (mime.type_()) == (mime::IMAGE) { + res = res.header(header::CACHE_CONTROL, "max-age=31536000"); + } else { + res = res.header(header::CACHE_CONTROL, "no-cache, no-store, must-revalidate"); + } + res.body(body).unwrap() } None => serve_path("200.html".to_owned()), }