Skip to content

Commit 13717b6

Browse files
Add short cache control for all paths
1 parent 6a417b9 commit 13717b6

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/main.rs

+10
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ async fn main() {
103103
.route_with_tsr("/ping/:edition/:hostname", get(ping_page))
104104
.route("/internal/ping-frame/:edition/:hostname", get(ping_frame))
105105
.route("/internal/ping-markup/:edition/:hostname", get(ping_markup))
106+
.layer(axum::middleware::from_fn(cache_short))
106107
.fallback_service(serve_dir)
107108
.layer(axum::middleware::from_fn(csp))
108109
.with_state(state);
@@ -127,6 +128,8 @@ static ROBOTS_NAME: HeaderName = HeaderName::from_static("x-robots-tag");
127128
static ROBOTS_VALUE: HeaderValue = HeaderValue::from_static("noindex");
128129
static CACHE_CONTROL_IMMUTABLE: HeaderValue =
129130
HeaderValue::from_static("immutable, public, max-age=31536000");
131+
static CACHE_CONTROL_SHORT: HeaderValue =
132+
HeaderValue::from_static("max-age=30, public, stale-while-revalidate");
130133

131134
static CSP_VALUE: HeaderValue = HeaderValue::from_static(
132135
"default-src 'self'; \
@@ -160,6 +163,13 @@ async fn cache(req: Request, next: Next) -> Response {
160163
resp
161164
}
162165

166+
async fn cache_short(req: Request, next: Next) -> Response {
167+
let mut resp = next.run(req).await;
168+
resp.headers_mut()
169+
.insert(CACHE_CONTROL, CACHE_CONTROL_SHORT.clone());
170+
resp
171+
}
172+
163173
#[derive(Template)]
164174
#[template(path = "index.html")]
165175
pub struct RootTemplate {

0 commit comments

Comments
 (0)