Skip to content

Commit 5a1ec5e

Browse files
jshajyn514
authored andcommitted
Make storage-change-detection.html cacheable.
1 parent 3e9a87f commit 5a1ec5e

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/web/page/web_page.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
use super::TemplateData;
22
use crate::ctry;
33
use crate::web::csp::Csp;
4-
use iron::{headers::ContentType, response::Response, status::Status, IronResult, Request};
4+
use iron::{
5+
headers::{CacheControl, ContentType},
6+
response::Response,
7+
status::Status,
8+
IronResult, Request,
9+
};
510
use serde::Serialize;
611
use std::borrow::Cow;
712
use tera::Context;
@@ -77,6 +82,7 @@ pub trait WebPage: Serialize + Sized {
7782

7883
let mut response = Response::with((status, rendered));
7984
response.headers.set(Self::content_type());
85+
response.headers.set(Self::cache_control());
8086

8187
Ok(response)
8288
}
@@ -93,4 +99,9 @@ pub trait WebPage: Serialize + Sized {
9399
fn content_type() -> ContentType {
94100
ContentType::html()
95101
}
102+
103+
/// The contents of the Cache-Control header. Defaults to no caching.
104+
fn cache_control() -> CacheControl {
105+
CacheControl(vec![])
106+
}
96107
}

src/web/routes.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
use crate::web::page::WebPage;
2+
13
use super::metrics::RequestRecorder;
2-
use iron::middleware::Handler;
4+
use ::std::borrow::Cow;
5+
use iron::{
6+
headers::{CacheControl, CacheDirective},
7+
middleware::Handler,
8+
};
39
use router::Router;
410
use std::collections::HashSet;
511

@@ -34,7 +40,15 @@ pub(super) fn build_routes() -> Routes {
3440
routes.internal_page("/-/storage-change-detection.html", {
3541
#[derive(Debug, serde::Serialize)]
3642
struct StorageChangeDetection {}
37-
crate::impl_webpage!(StorageChangeDetection = "storage-change-detection.html");
43+
44+
impl WebPage for StorageChangeDetection {
45+
fn template(&self) -> Cow<'static, str> {
46+
"storage-change-detection.html".into()
47+
}
48+
fn cache_control() -> CacheControl {
49+
CacheControl(vec![CacheDirective::MaxAge(604800)])
50+
}
51+
}
3852
fn storage_change_detection(req: &mut iron::Request) -> iron::IronResult<iron::Response> {
3953
crate::web::page::WebPage::into_response(StorageChangeDetection {}, req)
4054
}

0 commit comments

Comments
 (0)