Skip to content

Commit 37ea2b6

Browse files
committed
add option to disable the full page cache
1 parent 3444e85 commit 37ea2b6

File tree

4 files changed

+69
-9
lines changed

4 files changed

+69
-9
lines changed

src/cdn.rs

+5
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,11 @@ pub(crate) fn queue_crate_invalidation(
401401
config: &Config,
402402
name: &str,
403403
) -> Result<()> {
404+
if !config.full_page_cache {
405+
info!("full page cache disabled, skipping queueing invalidation");
406+
return Ok(());
407+
}
408+
404409
let mut add = |distribution_id: &str, path_patterns: &[&str]| -> Result<()> {
405410
for pattern in path_patterns {
406411
debug!(distribution_id, pattern, "enqueueing web CDN invalidation");

src/config.rs

+7
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ pub struct Config {
7777
// generate just that directive. Values are in seconds.
7878
pub(crate) cache_control_stale_while_revalidate: Option<u32>,
7979

80+
// Activate full page caching.
81+
// When disabled, we still cache static assets.
82+
// This only affects pages that depend on invalidations to work.
83+
pub(crate) full_page_cache: bool,
84+
8085
pub(crate) cdn_backend: CdnKind,
8186

8287
// CloudFront distribution ID for the web server.
@@ -174,6 +179,8 @@ impl Config {
174179
"CACHE_CONTROL_STALE_WHILE_REVALIDATE",
175180
)?,
176181

182+
full_page_cache: env("DOCSRS_FULL_PAGE_CACHE", true)?,
183+
177184
cdn_backend: env("DOCSRS_CDN_BACKEND", CdnKind::Dummy)?,
178185

179186
cloudfront_distribution_id_web: maybe_env("CLOUDFRONT_DISTRIBUTION_ID_WEB")?,

src/utils/daemon.rs

+5
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ pub fn start_background_cdn_invalidator(context: &dyn Context) -> Result<(), Err
109109
return Ok(());
110110
}
111111

112+
if !config.full_page_cache {
113+
info!("full page cache disabled, skipping background cdn invalidation");
114+
return Ok(());
115+
}
116+
112117
cron("cdn invalidator", Duration::from_secs(60), move || {
113118
let mut conn = pool.get()?;
114119
if let Some(distribution_id) = config.cloudfront_distribution_id_web.as_ref() {

src/web/cache.rs

+52-9
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,26 @@ impl CachePolicy {
4848
CachePolicy::NoStoreMustRevalidate => Some(NO_STORE_MUST_REVALIDATE.clone()),
4949
CachePolicy::ForeverInCdnAndBrowser => Some(FOREVER_IN_CDN_AND_BROWSER.clone()),
5050
CachePolicy::ForeverInCdn => {
51-
// A missing `max-age` or `s-maxage` in the Cache-Control header will lead to
52-
// CloudFront using the default TTL, while the browser not seeing any caching header.
53-
// This means we can have the CDN caching the documentation while just
54-
// issuing a purge after a build.
55-
// https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Expiration.html#ExpirationDownloadDist
56-
None
51+
if config.full_page_cache {
52+
// A missing `max-age` or `s-maxage` in the Cache-Control header will lead to
53+
// CloudFront using the default TTL, while the browser not seeing any caching header.
54+
// This means we can have the CDN caching the documentation while just
55+
// issuing a purge after a build.
56+
// https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Expiration.html#ExpirationDownloadDist
57+
None
58+
} else {
59+
Some(NO_CACHING.clone())
60+
}
61+
}
62+
CachePolicy::ForeverInCdnAndStaleInBrowser => {
63+
if config.full_page_cache {
64+
config
65+
.cache_control_stale_while_revalidate
66+
.map(|seconds| format!("stale-while-revalidate={seconds}").parse().unwrap())
67+
} else {
68+
Some(NO_CACHING.clone())
69+
}
5770
}
58-
CachePolicy::ForeverInCdnAndStaleInBrowser => config
59-
.cache_control_stale_while_revalidate
60-
.map(|seconds| format!("stale-while-revalidate={seconds}").parse().unwrap()),
6171
}
6272
}
6373
}
@@ -129,6 +139,7 @@ mod tests {
129139
Ok(())
130140
});
131141
}
142+
132143
#[test]
133144
fn render_stale_with_config() {
134145
wrapper(|env| {
@@ -145,4 +156,36 @@ mod tests {
145156
Ok(())
146157
});
147158
}
159+
160+
#[test]
161+
fn render_forever_in_cdn_disabled() {
162+
wrapper(|env| {
163+
env.override_config(|config| {
164+
config.full_page_cache = false;
165+
});
166+
167+
assert_eq!(
168+
CachePolicy::ForeverInCdn.render(&env.config()).unwrap(),
169+
"max-age=0"
170+
);
171+
Ok(())
172+
});
173+
}
174+
175+
#[test]
176+
fn render_forever_in_cdn_or_stale_disabled() {
177+
wrapper(|env| {
178+
env.override_config(|config| {
179+
config.full_page_cache = false;
180+
});
181+
182+
assert_eq!(
183+
CachePolicy::ForeverInCdnAndStaleInBrowser
184+
.render(&env.config())
185+
.unwrap(),
186+
"max-age=0"
187+
);
188+
Ok(())
189+
});
190+
}
148191
}

0 commit comments

Comments
 (0)