Allow caching all GET requests

get-cache
Felix Ableitner 2024-04-17 14:39:01 +02:00
parent 6efab9aab1
commit e8a5b9afe3
1 changed files with 5 additions and 4 deletions

View File

@ -1,7 +1,7 @@
use actix_web::{ use actix_web::{
body::MessageBody, body::MessageBody,
dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform}, dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform},
http::header::CACHE_CONTROL, http::header::CACHE_CONTROL,http::Method,
Error, Error,
HttpMessage, HttpMessage,
}; };
@ -77,16 +77,17 @@ where
} }
} }
let req_method = req.method().clone();
let mut res = svc.call(req).await?; let mut res = svc.call(req).await?;
// Add cache-control header if none is present // Add cache-control header if none is present
if !res.headers().contains_key(CACHE_CONTROL) { if !res.headers().contains_key(CACHE_CONTROL) {
// If user is authenticated, mark as private. Otherwise cache // If user is authenticated, mark as private. Otherwise cache
// up to one minute. // up to one minute.
let cache_value = if jwt.is_some() { let cache_value = if jwt.is_none() || req_method == Method::GET {
"private"
} else {
"public, max-age=60" "public, max-age=60"
} else {
"private"
}; };
res res
.headers_mut() .headers_mut()