From e8a5b9afe34c7bcb1304c6f39dc796b6c62a57be Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Wed, 17 Apr 2024 14:39:01 +0200 Subject: [PATCH] Allow caching all GET requests --- src/session_middleware.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/session_middleware.rs b/src/session_middleware.rs index f4535249c..68ad5df8c 100644 --- a/src/session_middleware.rs +++ b/src/session_middleware.rs @@ -1,7 +1,7 @@ use actix_web::{ body::MessageBody, dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform}, - http::header::CACHE_CONTROL, + http::header::CACHE_CONTROL,http::Method, Error, HttpMessage, }; @@ -77,16 +77,17 @@ where } } + let req_method = req.method().clone(); let mut res = svc.call(req).await?; // Add cache-control header if none is present if !res.headers().contains_key(CACHE_CONTROL) { // If user is authenticated, mark as private. Otherwise cache // up to one minute. - let cache_value = if jwt.is_some() { - "private" - } else { + let cache_value = if jwt.is_none() || req_method == Method::GET { "public, max-age=60" + } else { + "private" }; res .headers_mut()