Skip to content

Commit 61033df

Browse files
committed
allow setting s-maxage for pages too
1 parent 5a96fb3 commit 61033df

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
@@ -39,6 +39,7 @@ static DOC_RUST_LANG_ORG_REDIRECTS: Lazy<HashMap<&str, &str>> = Lazy::new(|| {
3939

4040
fn generate_cache_directives_for(
4141
max_age: Option<u32>,
42+
s_max_age: Option<u32>,
4243
stale_while_revalidate: Option<u32>,
4344
) -> Option<CacheControl> {
4445
let mut directives = vec![];
@@ -54,6 +55,10 @@ fn generate_cache_directives_for(
5455
directives.push(CacheDirective::MaxAge(seconds));
5556
}
5657

58+
if let Some(seconds) = s_max_age {
59+
directives.push(CacheDirective::SMaxAge(seconds));
60+
}
61+
5762
if !directives.is_empty() {
5863
Some(CacheControl(directives))
5964
} else {
@@ -285,11 +290,13 @@ impl RustdocPage {
285290
let cache_control = if is_latest_url {
286291
generate_cache_directives_for(
287292
Some(config.cache_control_max_age_latest.unwrap_or(0)),
293+
config.cache_control_s_max_age_latest,
288294
config.cache_control_stale_while_revalidate_latest,
289295
)
290296
} else {
291297
generate_cache_directives_for(
292298
config.cache_control_max_age,
299+
config.cache_control_s_max_age,
293300
config.cache_control_stale_while_revalidate,
294301
)
295302
};
@@ -973,7 +980,9 @@ mod test {
973980
wrapper(|env| {
974981
env.override_config(|config| {
975982
config.cache_control_max_age = Some(666);
983+
config.cache_control_s_max_age = Some(777);
976984
config.cache_control_max_age_latest = Some(999);
985+
config.cache_control_s_max_age_latest = Some(888);
977986
config.cache_control_stale_while_revalidate = Some(2222222);
978987
config.cache_control_stale_while_revalidate_latest = Some(3333333);
979988
});
@@ -991,15 +1000,15 @@ mod test {
9911000
let resp = web.get("/dummy/latest/dummy/").send()?;
9921001
assert_eq!(
9931002
resp.headers().get("Cache-Control").unwrap(),
994-
&"stale-while-revalidate=3333333, max-age=999"
1003+
&"stale-while-revalidate=3333333, max-age=999, s-maxage=888"
9951004
);
9961005
}
9971006

9981007
{
9991008
let resp = web.get("/dummy/0.1.0/dummy/").send()?;
10001009
assert_eq!(
10011010
resp.headers().get("Cache-Control").unwrap(),
1002-
&"stale-while-revalidate=2222222, max-age=666"
1011+
&"stale-while-revalidate=2222222, max-age=666, s-maxage=777"
10031012
);
10041013
}
10051014
Ok(())

0 commit comments

Comments
 (0)