Skip to content

Commit 9598ae7

Browse files
committed
Set CDN caching headers for rustdoc redirects
1 parent 9fe8f02 commit 9598ae7

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/config.rs

+6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub struct Config {
3838
// Time between 'git gc --auto' calls in seconds
3939
pub(crate) registry_gc_interval: u64,
4040

41+
<<<<<<< HEAD
4142
// random crate search generates a number of random IDs to
4243
// efficiently find a random crate with > 100 GH stars.
4344
// The amount depends on the ratio of crates with >100 stars
@@ -46,6 +47,10 @@ pub struct Config {
4647
// `500` for a ratio of 7249 over 54k crates.
4748
// For unit-tests the number has to be higher.
4849
pub(crate) random_crate_search_view_size: u32,
50+
=======
51+
// CDN / caching settings
52+
pub(crate) cache_rustdoc_redirects: u32,
53+
>>>>>>> 53d35de (Set CDN caching headers for rustdoc redirects)
4954

5055
// Build params
5156
pub(crate) build_attempts: u16,
@@ -94,6 +99,7 @@ impl Config {
9499
registry_gc_interval: env("DOCSRS_REGISTRY_GC_INTERVAL", 60 * 60)?,
95100

96101
random_crate_search_view_size: env("DOCSRS_RANDOM_CRATE_SEARCH_VIEW_SIZE", 500)?,
102+
cache_rustdoc_redirects: env("DOCSRS_CACHE_RUSTDOC_REDIRECTS", 30 * 60)?,
97103

98104
rustwide_workspace: env("CRATESFYI_RUSTWIDE_WORKSPACE", PathBuf::from(".workspace"))?,
99105
inside_docker: env("DOCS_RS_DOCKER", false)?,

src/web/rustdoc.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,13 @@ pub fn rustdoc_redirector_handler(req: &mut Request) -> IronResult<Response> {
6969
url_str.push_str(query);
7070
}
7171
let url = ctry!(req, Url::parse(&url_str));
72+
let config = extension!(req, Config);
7273
let mut resp = Response::with((status::Found, Redirect(url)));
73-
resp.headers.set(Expires(HttpDate(time::now())));
74+
resp.headers.set(CacheControl(vec![
75+
CacheDirective::MaxAge(0),
76+
// s-maxage is only for the CDN.
77+
CacheDirective::SMaxAge(config.cache_rustdoc_redirects),
78+
]));
7479

7580
Ok(resp)
7681
}
@@ -81,8 +86,13 @@ pub fn rustdoc_redirector_handler(req: &mut Request) -> IronResult<Response> {
8186
Url::parse(&format!("{}/crate/{}/{}", redirect_base(req), name, vers)),
8287
);
8388

89+
let config = extension!(req, Config);
8490
let mut resp = Response::with((status::Found, Redirect(url)));
85-
resp.headers.set(Expires(HttpDate(time::now())));
91+
resp.headers.set(CacheControl(vec![
92+
CacheDirective::MaxAge(0),
93+
// s-maxage is only for the CDN.
94+
CacheDirective::SMaxAge(config.cache_rustdoc_redirects),
95+
]));
8696

8797
Ok(resp)
8898
}

0 commit comments

Comments
 (0)