Skip to content

Commit ab43e78

Browse files
jshasyphar
authored andcommitted
Make Cache-Control configurable.
1 parent 95c34a7 commit ab43e78

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

src/config.rs

Lines changed: 11 additions & 0 deletions
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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,13 @@ impl RustdocPage {
254254
let is_latest_url = self.is_latest_url;
255255
// Build the page of documentation
256256
let ctx = ctry!(req, tera::Context::from_serialize(self));
257+
let config = extension!(req, Config);
257258
// Extract the head and body of the rustdoc file so that we can insert it into our own html
258259
// while logging OOM errors from html rewriting
259260
let html = match utils::rewrite_lol(rustdoc_html, max_parse_memory, ctx, templates) {
260261
Err(RewritingError::MemoryLimitExceeded(..)) => {
261262
metrics.html_rewrite_ooms.inc();
262263

263-
let config = extension!(req, Config);
264264
let err = anyhow!(
265265
"Failed to serve the rustdoc file '{}' because rewriting it surpassed the memory limit of {} bytes",
266266
file_path, config.max_parse_memory,
@@ -278,13 +278,21 @@ impl RustdocPage {
278278
.headers
279279
.set(CacheControl(vec![CacheDirective::MaxAge(0)]));
280280
} else {
281-
response.headers.set(CacheControl(vec![
282-
CacheDirective::Extension(
281+
let mut directives = vec![];
282+
if let Some(seconds) = config.cache_control_stale_while_revalidate {
283+
directives.push(CacheDirective::Extension(
283284
"stale-while-revalidate".to_string(),
284-
Some("2592000".to_string()), // sixty days
285-
),
286-
CacheDirective::MaxAge(600u32), // ten minutes
287-
]));
285+
Some(format!("{}", seconds)),
286+
));
287+
}
288+
289+
if let Some(seconds) = config.cache_control_max_age {
290+
directives.push(CacheDirective::MaxAge(seconds));
291+
}
292+
293+
if !directives.is_empty() {
294+
response.headers.set(CacheControl(directives));
295+
}
288296
}
289297
Ok(response)
290298
}
@@ -895,13 +903,17 @@ mod test {
895903
#[test]
896904
fn cache_headers() {
897905
wrapper(|env| {
906+
env.override_config(|config| {
907+
config.cache_control_max_age = Some(600);
908+
config.cache_control_stale_while_revalidate = Some(2592000);
909+
});
910+
898911
env.fake_release()
899912
.name("dummy")
900913
.version("0.1.0")
901914
.archive_storage(true)
902915
.rustdoc_file("dummy/index.html")
903916
.create()?;
904-
905917
let resp = env.frontend().get("/dummy/latest/dummy/").send()?;
906918
assert_eq!(resp.headers().get("Cache-Control").unwrap(), &"max-age=0");
907919

0 commit comments

Comments
 (0)