@@ -254,13 +254,13 @@ impl RustdocPage {
254
254
let is_latest_url = self . is_latest_url ;
255
255
// Build the page of documentation
256
256
let ctx = ctry ! ( req, tera:: Context :: from_serialize( self ) ) ;
257
+ let config = extension ! ( req, Config ) ;
257
258
// Extract the head and body of the rustdoc file so that we can insert it into our own html
258
259
// while logging OOM errors from html rewriting
259
260
let html = match utils:: rewrite_lol ( rustdoc_html, max_parse_memory, ctx, templates) {
260
261
Err ( RewritingError :: MemoryLimitExceeded ( ..) ) => {
261
262
metrics. html_rewrite_ooms . inc ( ) ;
262
263
263
- let config = extension ! ( req, Config ) ;
264
264
let err = anyhow ! (
265
265
"Failed to serve the rustdoc file '{}' because rewriting it surpassed the memory limit of {} bytes" ,
266
266
file_path, config. max_parse_memory,
@@ -278,13 +278,21 @@ impl RustdocPage {
278
278
. headers
279
279
. set ( CacheControl ( vec ! [ CacheDirective :: MaxAge ( 0 ) ] ) ) ;
280
280
} else {
281
- response. headers . set ( CacheControl ( vec ! [
282
- CacheDirective :: Extension (
281
+ let mut directives = vec ! [ ] ;
282
+ if let Some ( seconds) = config. cache_control_stale_while_revalidate {
283
+ directives. push ( CacheDirective :: Extension (
283
284
"stale-while-revalidate" . to_string ( ) ,
284
- Some ( "2592000" . to_string( ) ) , // sixty days
285
- ) ,
286
- CacheDirective :: MaxAge ( 600u32 ) , // ten minutes
287
- ] ) ) ;
285
+ Some ( format ! ( "{}" , seconds) ) ,
286
+ ) ) ;
287
+ }
288
+
289
+ if let Some ( seconds) = config. cache_control_max_age {
290
+ directives. push ( CacheDirective :: MaxAge ( seconds) ) ;
291
+ }
292
+
293
+ if !directives. is_empty ( ) {
294
+ response. headers . set ( CacheControl ( directives) ) ;
295
+ }
288
296
}
289
297
Ok ( response)
290
298
}
@@ -895,13 +903,17 @@ mod test {
895
903
#[ test]
896
904
fn cache_headers ( ) {
897
905
wrapper ( |env| {
906
+ env. override_config ( |config| {
907
+ config. cache_control_max_age = Some ( 600 ) ;
908
+ config. cache_control_stale_while_revalidate = Some ( 2592000 ) ;
909
+ } ) ;
910
+
898
911
env. fake_release ( )
899
912
. name ( "dummy" )
900
913
. version ( "0.1.0" )
901
914
. archive_storage ( true )
902
915
. rustdoc_file ( "dummy/index.html" )
903
916
. create ( ) ?;
904
-
905
917
let resp = env. frontend ( ) . get ( "/dummy/latest/dummy/" ) . send ( ) ?;
906
918
assert_eq ! ( resp. headers( ) . get( "Cache-Control" ) . unwrap( ) , & "max-age=0" ) ;
907
919
0 commit comments