From 0b4ed164b5b5d38f403b56c5adc241412e68399a Mon Sep 17 00:00:00 2001 From: Denis Cornehl Date: Sat, 24 Sep 2022 19:51:34 +0200 Subject: [PATCH] remove latest-version caching logic in favor for new full page caching --- src/config.rs | 14 ----- src/web/rustdoc.rs | 135 +++++++-------------------------------------- 2 files changed, 19 insertions(+), 130 deletions(-) diff --git a/src/config.rs b/src/config.rs index 4a3dcf16d..5e9d5a540 100644 --- a/src/config.rs +++ b/src/config.rs @@ -62,13 +62,6 @@ pub struct Config { // generate just that directive. Values are in seconds. pub(crate) cache_control_stale_while_revalidate: Option, pub(crate) cache_control_max_age: Option, - pub(crate) cache_control_s_max_age: Option, - - // Cache-Control header, for /latest/ URLs. - // Same conditions as above apply. - pub(crate) cache_control_stale_while_revalidate_latest: Option, - pub(crate) cache_control_max_age_latest: Option, - pub(crate) cache_control_s_max_age_latest: Option, pub(crate) cdn_backend: CdnKind, @@ -153,13 +146,6 @@ impl Config { "CACHE_CONTROL_STALE_WHILE_REVALIDATE", )?, cache_control_max_age: maybe_env("CACHE_CONTROL_MAX_AGE")?, - cache_control_s_max_age: maybe_env("CACHE_CONTROL_S_MAX_AGE")?, - - cache_control_stale_while_revalidate_latest: maybe_env( - "CACHE_CONTROL_STALE_WHILE_REVALIDATE_LATEST", - )?, - cache_control_max_age_latest: maybe_env("CACHE_CONTROL_MAX_AGE_LATEST")?, - cache_control_s_max_age_latest: maybe_env("CACHE_CONTROL_S_MAX_AGE_LATEST")?, cdn_backend: env("DOCSRS_CDN_BACKEND", CdnKind::Dummy)?, diff --git a/src/web/rustdoc.rs b/src/web/rustdoc.rs index aa7d185c1..b6de294f2 100644 --- a/src/web/rustdoc.rs +++ b/src/web/rustdoc.rs @@ -37,35 +37,6 @@ static DOC_RUST_LANG_ORG_REDIRECTS: Lazy> = Lazy::new(|| { ]) }); -fn generate_cache_directives_for( - max_age: Option, - s_max_age: Option, - stale_while_revalidate: Option, -) -> Option { - let mut directives = vec![]; - - if let Some(seconds) = stale_while_revalidate { - directives.push(CacheDirective::Extension( - "stale-while-revalidate".to_string(), - Some(format!("{}", seconds)), - )); - } - - if let Some(seconds) = max_age { - directives.push(CacheDirective::MaxAge(seconds)); - } - - if let Some(seconds) = s_max_age { - directives.push(CacheDirective::SMaxAge(seconds)); - } - - if !directives.is_empty() { - Some(CacheControl(directives)) - } else { - None - } -} - /// Handler called for `/:crate` and `/:crate/:version` URLs. Automatically redirects to the docs /// or crate details page based on whether the given crate version was successfully built. pub fn rustdoc_redirector_handler(req: &mut Request) -> IronResult { @@ -287,21 +258,26 @@ impl RustdocPage { let mut response = Response::with((Status::Ok, html)); response.headers.set(ContentType::html()); - let cache_control = if is_latest_url { - generate_cache_directives_for( - Some(config.cache_control_max_age_latest.unwrap_or(0)), - config.cache_control_s_max_age_latest, - config.cache_control_stale_while_revalidate_latest, - ) + if is_latest_url { + response + .headers + .set(CacheControl(vec![CacheDirective::MaxAge(0)])); } else { - generate_cache_directives_for( - config.cache_control_max_age, - config.cache_control_s_max_age, - config.cache_control_stale_while_revalidate, - ) - }; - if let Some(cache_control) = cache_control { - response.headers.set(cache_control); + let mut directives = vec![]; + if let Some(seconds) = config.cache_control_stale_while_revalidate { + directives.push(CacheDirective::Extension( + "stale-while-revalidate".to_string(), + Some(format!("{}", seconds)), + )); + } + + if let Some(seconds) = config.cache_control_max_age { + directives.push(CacheDirective::MaxAge(seconds)); + } + + if !directives.is_empty() { + response.headers.set(CacheControl(directives)); + } } Ok(response) } @@ -909,39 +885,6 @@ mod test { }) } - #[test] - fn cache_headers_only_latest() { - wrapper(|env| { - env.override_config(|config| { - config.cache_control_max_age_latest = Some(600); - config.cache_control_stale_while_revalidate_latest = Some(2592000); - }); - - env.fake_release() - .name("dummy") - .version("0.1.0") - .archive_storage(true) - .rustdoc_file("dummy/index.html") - .create()?; - - let web = env.frontend(); - - { - let resp = web.get("/dummy/latest/dummy/").send()?; - assert_eq!( - resp.headers().get("Cache-Control").unwrap(), - &"stale-while-revalidate=2592000, max-age=600" - ); - } - - { - let resp = web.get("/dummy/0.1.0/dummy/").send()?; - assert!(resp.headers().get("Cache-Control").is_none()); - } - Ok(()) - }) - } - #[test] fn cache_headers_on_version() { wrapper(|env| { @@ -975,46 +918,6 @@ mod test { }) } - #[test] - fn cache_headers_latest_and_version() { - wrapper(|env| { - env.override_config(|config| { - config.cache_control_max_age = Some(666); - config.cache_control_s_max_age = Some(777); - config.cache_control_max_age_latest = Some(999); - config.cache_control_s_max_age_latest = Some(888); - config.cache_control_stale_while_revalidate = Some(2222222); - config.cache_control_stale_while_revalidate_latest = Some(3333333); - }); - - env.fake_release() - .name("dummy") - .version("0.1.0") - .archive_storage(true) - .rustdoc_file("dummy/index.html") - .create()?; - - let web = env.frontend(); - - { - let resp = web.get("/dummy/latest/dummy/").send()?; - assert_eq!( - resp.headers().get("Cache-Control").unwrap(), - &"stale-while-revalidate=3333333, max-age=999, s-maxage=888" - ); - } - - { - let resp = web.get("/dummy/0.1.0/dummy/").send()?; - assert_eq!( - resp.headers().get("Cache-Control").unwrap(), - &"stale-while-revalidate=2222222, max-age=666, s-maxage=777" - ); - } - Ok(()) - }) - } - #[test_case(true)] #[test_case(false)] fn go_to_latest_version(archive_storage: bool) {