Skip to content

Commit 0b4ed16

Browse files
committed
remove latest-version caching logic in favor for new full page caching
1 parent 420f88d commit 0b4ed16

File tree

2 files changed

+19
-130
lines changed

2 files changed

+19
-130
lines changed

src/config.rs

-14
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,6 @@ 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>,
66-
67-
// Cache-Control header, for /latest/ URLs.
68-
// Same conditions as above apply.
69-
pub(crate) cache_control_stale_while_revalidate_latest: Option<u32>,
70-
pub(crate) cache_control_max_age_latest: Option<u32>,
71-
pub(crate) cache_control_s_max_age_latest: Option<u32>,
7265

7366
pub(crate) cdn_backend: CdnKind,
7467

@@ -153,13 +146,6 @@ impl Config {
153146
"CACHE_CONTROL_STALE_WHILE_REVALIDATE",
154147
)?,
155148
cache_control_max_age: maybe_env("CACHE_CONTROL_MAX_AGE")?,
156-
cache_control_s_max_age: maybe_env("CACHE_CONTROL_S_MAX_AGE")?,
157-
158-
cache_control_stale_while_revalidate_latest: maybe_env(
159-
"CACHE_CONTROL_STALE_WHILE_REVALIDATE_LATEST",
160-
)?,
161-
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")?,
163149

164150
cdn_backend: env("DOCSRS_CDN_BACKEND", CdnKind::Dummy)?,
165151

src/web/rustdoc.rs

+19-116
Original file line numberDiff line numberDiff line change
@@ -37,35 +37,6 @@ static DOC_RUST_LANG_ORG_REDIRECTS: Lazy<HashMap<&str, &str>> = Lazy::new(|| {
3737
])
3838
});
3939

40-
fn generate_cache_directives_for(
41-
max_age: Option<u32>,
42-
s_max_age: Option<u32>,
43-
stale_while_revalidate: Option<u32>,
44-
) -> Option<CacheControl> {
45-
let mut directives = vec![];
46-
47-
if let Some(seconds) = stale_while_revalidate {
48-
directives.push(CacheDirective::Extension(
49-
"stale-while-revalidate".to_string(),
50-
Some(format!("{}", seconds)),
51-
));
52-
}
53-
54-
if let Some(seconds) = max_age {
55-
directives.push(CacheDirective::MaxAge(seconds));
56-
}
57-
58-
if let Some(seconds) = s_max_age {
59-
directives.push(CacheDirective::SMaxAge(seconds));
60-
}
61-
62-
if !directives.is_empty() {
63-
Some(CacheControl(directives))
64-
} else {
65-
None
66-
}
67-
}
68-
6940
/// Handler called for `/:crate` and `/:crate/:version` URLs. Automatically redirects to the docs
7041
/// or crate details page based on whether the given crate version was successfully built.
7142
pub fn rustdoc_redirector_handler(req: &mut Request) -> IronResult<Response> {
@@ -287,21 +258,26 @@ impl RustdocPage {
287258
let mut response = Response::with((Status::Ok, html));
288259
response.headers.set(ContentType::html());
289260

290-
let cache_control = if is_latest_url {
291-
generate_cache_directives_for(
292-
Some(config.cache_control_max_age_latest.unwrap_or(0)),
293-
config.cache_control_s_max_age_latest,
294-
config.cache_control_stale_while_revalidate_latest,
295-
)
261+
if is_latest_url {
262+
response
263+
.headers
264+
.set(CacheControl(vec![CacheDirective::MaxAge(0)]));
296265
} else {
297-
generate_cache_directives_for(
298-
config.cache_control_max_age,
299-
config.cache_control_s_max_age,
300-
config.cache_control_stale_while_revalidate,
301-
)
302-
};
303-
if let Some(cache_control) = cache_control {
304-
response.headers.set(cache_control);
266+
let mut directives = vec![];
267+
if let Some(seconds) = config.cache_control_stale_while_revalidate {
268+
directives.push(CacheDirective::Extension(
269+
"stale-while-revalidate".to_string(),
270+
Some(format!("{}", seconds)),
271+
));
272+
}
273+
274+
if let Some(seconds) = config.cache_control_max_age {
275+
directives.push(CacheDirective::MaxAge(seconds));
276+
}
277+
278+
if !directives.is_empty() {
279+
response.headers.set(CacheControl(directives));
280+
}
305281
}
306282
Ok(response)
307283
}
@@ -909,39 +885,6 @@ mod test {
909885
})
910886
}
911887

912-
#[test]
913-
fn cache_headers_only_latest() {
914-
wrapper(|env| {
915-
env.override_config(|config| {
916-
config.cache_control_max_age_latest = Some(600);
917-
config.cache_control_stale_while_revalidate_latest = Some(2592000);
918-
});
919-
920-
env.fake_release()
921-
.name("dummy")
922-
.version("0.1.0")
923-
.archive_storage(true)
924-
.rustdoc_file("dummy/index.html")
925-
.create()?;
926-
927-
let web = env.frontend();
928-
929-
{
930-
let resp = web.get("/dummy/latest/dummy/").send()?;
931-
assert_eq!(
932-
resp.headers().get("Cache-Control").unwrap(),
933-
&"stale-while-revalidate=2592000, max-age=600"
934-
);
935-
}
936-
937-
{
938-
let resp = web.get("/dummy/0.1.0/dummy/").send()?;
939-
assert!(resp.headers().get("Cache-Control").is_none());
940-
}
941-
Ok(())
942-
})
943-
}
944-
945888
#[test]
946889
fn cache_headers_on_version() {
947890
wrapper(|env| {
@@ -975,46 +918,6 @@ mod test {
975918
})
976919
}
977920

978-
#[test]
979-
fn cache_headers_latest_and_version() {
980-
wrapper(|env| {
981-
env.override_config(|config| {
982-
config.cache_control_max_age = Some(666);
983-
config.cache_control_s_max_age = Some(777);
984-
config.cache_control_max_age_latest = Some(999);
985-
config.cache_control_s_max_age_latest = Some(888);
986-
config.cache_control_stale_while_revalidate = Some(2222222);
987-
config.cache_control_stale_while_revalidate_latest = Some(3333333);
988-
});
989-
990-
env.fake_release()
991-
.name("dummy")
992-
.version("0.1.0")
993-
.archive_storage(true)
994-
.rustdoc_file("dummy/index.html")
995-
.create()?;
996-
997-
let web = env.frontend();
998-
999-
{
1000-
let resp = web.get("/dummy/latest/dummy/").send()?;
1001-
assert_eq!(
1002-
resp.headers().get("Cache-Control").unwrap(),
1003-
&"stale-while-revalidate=3333333, max-age=999, s-maxage=888"
1004-
);
1005-
}
1006-
1007-
{
1008-
let resp = web.get("/dummy/0.1.0/dummy/").send()?;
1009-
assert_eq!(
1010-
resp.headers().get("Cache-Control").unwrap(),
1011-
&"stale-while-revalidate=2222222, max-age=666, s-maxage=777"
1012-
);
1013-
}
1014-
Ok(())
1015-
})
1016-
}
1017-
1018921
#[test_case(true)]
1019922
#[test_case(false)]
1020923
fn go_to_latest_version(archive_storage: bool) {

0 commit comments

Comments
 (0)