@@ -235,13 +235,13 @@ impl RustdocPage {
235235 let is_latest_url = self . is_latest_url ;
236236 // Build the page of documentation
237237 let ctx = ctry ! ( req, tera:: Context :: from_serialize( self ) ) ;
238+ let config = extension ! ( req, Config ) ;
238239 // Extract the head and body of the rustdoc file so that we can insert it into our own html
239240 // while logging OOM errors from html rewriting
240241 let html = match utils:: rewrite_lol ( rustdoc_html, max_parse_memory, ctx, templates) {
241242 Err ( RewritingError :: MemoryLimitExceeded ( ..) ) => {
242243 metrics. html_rewrite_ooms . inc ( ) ;
243244
244- let config = extension ! ( req, Config ) ;
245245 let err = anyhow ! (
246246 "Failed to serve the rustdoc file '{}' because rewriting it surpassed the memory limit of {} bytes" ,
247247 file_path, config. max_parse_memory,
@@ -259,13 +259,21 @@ impl RustdocPage {
259259 . headers
260260 . set ( CacheControl ( vec ! [ CacheDirective :: MaxAge ( 0 ) ] ) ) ;
261261 } else {
262- response. headers . set ( CacheControl ( vec ! [
263- CacheDirective :: Extension (
262+ let mut directives = vec ! [ ] ;
263+ if let Some ( seconds) = config. cache_control_stale_while_revalidate {
264+ directives. push ( CacheDirective :: Extension (
264265 "stale-while-revalidate" . to_string ( ) ,
265- Some ( "2592000" . to_string( ) ) , // sixty days
266- ) ,
267- CacheDirective :: MaxAge ( 600u32 ) , // ten minutes
268- ] ) ) ;
266+ Some ( format ! ( "{}" , seconds) ) ,
267+ ) ) ;
268+ }
269+
270+ if let Some ( seconds) = config. cache_control_max_age {
271+ directives. push ( CacheDirective :: MaxAge ( seconds) ) ;
272+ }
273+
274+ if !directives. is_empty ( ) {
275+ response. headers . set ( CacheControl ( directives) ) ;
276+ }
269277 }
270278 Ok ( response)
271279 }
@@ -864,13 +872,17 @@ mod test {
864872 #[ test]
865873 fn cache_headers ( ) {
866874 wrapper ( |env| {
875+ env. override_config ( |config| {
876+ config. cache_control_max_age = Some ( 600 ) ;
877+ config. cache_control_stale_while_revalidate = Some ( 2592000 ) ;
878+ } ) ;
879+
867880 env. fake_release ( )
868881 . name ( "dummy" )
869882 . version ( "0.1.0" )
870883 . archive_storage ( true )
871884 . rustdoc_file ( "dummy/index.html" )
872885 . create ( ) ?;
873-
874886 let resp = env. frontend ( ) . get ( "/dummy/latest/dummy/" ) . send ( ) ?;
875887 assert_eq ! ( resp. headers( ) . get( "Cache-Control" ) . unwrap( ) , & "max-age=0" ) ;
876888
0 commit comments