Skip to content

Commit 335712c

Browse files
committed
allow setting s-maxage for pages too
1 parent d032700 commit 335712c

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,13 @@ pub struct Config {
6262
// generate just that directive. Values are in seconds.
6363
pub(crate) cache_control_stale_while_revalidate: Option<u32>,
6464
pub(crate) cache_control_max_age: Option<u32>,
65+
pub(crate) cache_control_s_max_age: Option<u32>,
6566

6667
// Cache-Control header, for /latest/ URLs.
6768
// Same conditions as above apply.
6869
pub(crate) cache_control_stale_while_revalidate_latest: Option<u32>,
6970
pub(crate) cache_control_max_age_latest: Option<u32>,
71+
pub(crate) cache_control_s_max_age_latest: Option<u32>,
7072

7173
pub(crate) cdn_backend: CdnKind,
7274

@@ -151,11 +153,13 @@ impl Config {
151153
"CACHE_CONTROL_STALE_WHILE_REVALIDATE",
152154
)?,
153155
cache_control_max_age: maybe_env("CACHE_CONTROL_MAX_AGE")?,
156+
cache_control_s_max_age: maybe_env("CACHE_CONTROL_S_MAX_AGE")?,
154157

155158
cache_control_stale_while_revalidate_latest: maybe_env(
156159
"CACHE_CONTROL_STALE_WHILE_REVALIDATE_LATEST",
157160
)?,
158161
cache_control_max_age_latest: maybe_env("CACHE_CONTROL_MAX_AGE_LATEST")?,
162+
cache_control_s_max_age_latest: maybe_env("CACHE_CONTROL_S_MAX_AGE_LATEST")?,
159163

160164
cdn_backend: env("DOCSRS_CDN_BACKEND", CdnKind::Dummy)?,
161165

src/web/rustdoc.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ static DOC_RUST_LANG_ORG_REDIRECTS: Lazy<HashMap<&str, &str>> = Lazy::new(|| {
4444

4545
fn generate_cache_directives_for(
4646
max_age: Option<u32>,
47+
s_max_age: Option<u32>,
4748
stale_while_revalidate: Option<u32>,
4849
) -> Option<CacheControl> {
4950
let mut directives = vec![];
@@ -59,6 +60,10 @@ fn generate_cache_directives_for(
5960
directives.push(CacheDirective::MaxAge(seconds));
6061
}
6162

63+
if let Some(seconds) = s_max_age {
64+
directives.push(CacheDirective::SMaxAge(seconds));
65+
}
66+
6267
if !directives.is_empty() {
6368
Some(CacheControl(directives))
6469
} else {
@@ -288,11 +293,13 @@ impl RustdocPage {
288293
let cache_control = if is_latest_url {
289294
generate_cache_directives_for(
290295
Some(config.cache_control_max_age_latest.unwrap_or(0)),
296+
config.cache_control_s_max_age_latest,
291297
config.cache_control_stale_while_revalidate_latest,
292298
)
293299
} else {
294300
generate_cache_directives_for(
295301
config.cache_control_max_age,
302+
config.cache_control_s_max_age,
296303
config.cache_control_stale_while_revalidate,
297304
)
298305
};
@@ -976,7 +983,9 @@ mod test {
976983
wrapper(|env| {
977984
env.override_config(|config| {
978985
config.cache_control_max_age = Some(666);
986+
config.cache_control_s_max_age = Some(777);
979987
config.cache_control_max_age_latest = Some(999);
988+
config.cache_control_s_max_age_latest = Some(888);
980989
config.cache_control_stale_while_revalidate = Some(2222222);
981990
config.cache_control_stale_while_revalidate_latest = Some(3333333);
982991
});
@@ -994,15 +1003,15 @@ mod test {
9941003
let resp = web.get("/dummy/latest/dummy/").send()?;
9951004
assert_eq!(
9961005
resp.headers().get("Cache-Control").unwrap(),
997-
&"stale-while-revalidate=3333333, max-age=999"
1006+
&"stale-while-revalidate=3333333, max-age=999, s-maxage=888"
9981007
);
9991008
}
10001009

10011010
{
10021011
let resp = web.get("/dummy/0.1.0/dummy/").send()?;
10031012
assert_eq!(
10041013
resp.headers().get("Cache-Control").unwrap(),
1005-
&"stale-while-revalidate=2222222, max-age=666"
1014+
&"stale-while-revalidate=2222222, max-age=666, s-maxage=777"
10061015
);
10071016
}
10081017
Ok(())

0 commit comments

Comments
 (0)