set a more realistic cache-control for URL summary
Signed-off-by: eternal-flame-AD <yume@yumechi.jp>
This commit is contained in:
parent
e6afa180bb
commit
791c0183c9
1 changed files with 18 additions and 11 deletions
29
src/lib.rs
29
src/lib.rs
|
@ -19,7 +19,7 @@ use axum::extract::ConnectInfo;
|
||||||
use axum::{
|
use axum::{
|
||||||
body::Body,
|
body::Body,
|
||||||
extract::{Path, Query, State},
|
extract::{Path, Query, State},
|
||||||
http::{self, HeaderMap, StatusCode},
|
http::{self, HeaderMap, HeaderValue, StatusCode},
|
||||||
response::{IntoResponse, Redirect, Response},
|
response::{IntoResponse, Redirect, Response},
|
||||||
routing::get,
|
routing::get,
|
||||||
Json, Router,
|
Json, Router,
|
||||||
|
@ -250,20 +250,27 @@ pub async fn set_cache_control<
|
||||||
next: axum::middleware::Next,
|
next: axum::middleware::Next,
|
||||||
) -> Response {
|
) -> Response {
|
||||||
let mut resp = next.run(request).await;
|
let mut resp = next.run(request).await;
|
||||||
|
let ok = resp.status().is_success();
|
||||||
|
let headers = resp.headers_mut();
|
||||||
if enabled {
|
if enabled {
|
||||||
if resp.status() == StatusCode::OK {
|
if ok {
|
||||||
let headers = resp.headers_mut();
|
if headers.get("content-type").map_or(false, |x| {
|
||||||
headers.insert(
|
[b"image/", b"video/", b"audio/"]
|
||||||
"Cache-Control",
|
.into_iter()
|
||||||
"public, max-age=31536000, immutable".parse().unwrap(),
|
.any(|y| x.as_bytes().starts_with(y))
|
||||||
);
|
}) {
|
||||||
|
headers.insert(
|
||||||
|
"cache-control",
|
||||||
|
HeaderValue::from_static("public, max-age=31536000, immutable"),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
headers.insert("cache-control", HeaderValue::from_static("max-age=86400"));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
let headers = resp.headers_mut();
|
headers.insert("cache-control", HeaderValue::from_static("max-age=300"));
|
||||||
headers.insert("Cache-Control", "max-age=300".parse().unwrap());
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let headers = resp.headers_mut();
|
headers.insert("cache-control", HeaderValue::from_static("no-store"));
|
||||||
headers.insert("Cache-Control", "no-store".parse().unwrap());
|
|
||||||
}
|
}
|
||||||
resp
|
resp
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue