Skip to content

Commit 8b71c52

Browse files
committed
Make Cache-Control configurable.
1 parent a854363 commit 8b71c52

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

src/config.rs

+11
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ pub struct Config {
5757
// Content Security Policy
5858
pub(crate) csp_report_only: bool,
5959

60+
// Cache-Control header
61+
// If both are absent, don't generate the header. If only one is present,
62+
// generate just that directive. Values are in seconds.
63+
pub(crate) cache_control_stale_while_revalidate: Option<u32>,
64+
pub(crate) cache_control_max_age: Option<u32>,
65+
6066
// Build params
6167
pub(crate) build_attempts: u16,
6268
pub(crate) rustwide_workspace: PathBuf,
@@ -130,6 +136,11 @@ impl Config {
130136

131137
csp_report_only: env("DOCSRS_CSP_REPORT_ONLY", false)?,
132138

139+
cache_control_stale_while_revalidate: maybe_env(
140+
"CACHE_CONTROL_STALE_WHILE_REVALIDATE",
141+
)?,
142+
cache_control_max_age: maybe_env("CACHE_CONTROL_MAX_AGE")?,
143+
133144
local_archive_cache_path: env(
134145
"DOCSRS_ARCHIVE_INDEX_CACHE_PATH",
135146
prefix.join("archive_cache"),

src/web/rustdoc.rs

+20-8
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,13 @@ impl RustdocPage {
235235
let is_latest_url = self.is_latest_url;
236236
// Build the page of documentation
237237
let ctx = ctry!(req, tera::Context::from_serialize(self));
238+
let config = extension!(req, Config);
238239
// Extract the head and body of the rustdoc file so that we can insert it into our own html
239240
// while logging OOM errors from html rewriting
240241
let html = match utils::rewrite_lol(rustdoc_html, max_parse_memory, ctx, templates) {
241242
Err(RewritingError::MemoryLimitExceeded(..)) => {
242243
metrics.html_rewrite_ooms.inc();
243244

244-
let config = extension!(req, Config);
245245
let err = anyhow!(
246246
"Failed to serve the rustdoc file '{}' because rewriting it surpassed the memory limit of {} bytes",
247247
file_path, config.max_parse_memory,
@@ -259,13 +259,21 @@ impl RustdocPage {
259259
.headers
260260
.set(CacheControl(vec![CacheDirective::MaxAge(0)]));
261261
} else {
262-
response.headers.set(CacheControl(vec![
263-
CacheDirective::Extension(
262+
let mut directives = vec![];
263+
if let Some(seconds) = config.cache_control_stale_while_revalidate {
264+
directives.push(CacheDirective::Extension(
264265
"stale-while-revalidate".to_string(),
265-
Some("2592000".to_string()), // sixty days
266-
),
267-
CacheDirective::MaxAge(600u32), // ten minutes
268-
]));
266+
Some(format!("{}", seconds)),
267+
));
268+
}
269+
270+
if let Some(seconds) = config.cache_control_max_age {
271+
directives.push(CacheDirective::MaxAge(seconds));
272+
}
273+
274+
if !directives.is_empty() {
275+
response.headers.set(CacheControl(directives));
276+
}
269277
}
270278
Ok(response)
271279
}
@@ -864,13 +872,17 @@ mod test {
864872
#[test]
865873
fn cache_headers() {
866874
wrapper(|env| {
875+
env.override_config(|config| {
876+
config.cache_control_max_age=Some(600);
877+
config.cache_control_stale_while_revalidate=Some(2592000);
878+
});
879+
867880
env.fake_release()
868881
.name("dummy")
869882
.version("0.1.0")
870883
.archive_storage(true)
871884
.rustdoc_file("dummy/index.html")
872885
.create()?;
873-
874886
let resp = env.frontend().get("/dummy/latest/dummy/").send()?;
875887
assert_eq!(resp.headers().get("Cache-Control").unwrap(), &"max-age=0");
876888

0 commit comments

Comments
 (0)