@@ -254,13 +254,13 @@ impl RustdocPage {
254254 let is_latest_url = self . is_latest_url ;
255255 // Build the page of documentation
256256 let ctx = ctry ! ( req, tera:: Context :: from_serialize( self ) ) ;
257+ let config = extension ! ( req, Config ) ;
257258 // Extract the head and body of the rustdoc file so that we can insert it into our own html
258259 // while logging OOM errors from html rewriting
259260 let html = match utils:: rewrite_lol ( rustdoc_html, max_parse_memory, ctx, templates) {
260261 Err ( RewritingError :: MemoryLimitExceeded ( ..) ) => {
261262 metrics. html_rewrite_ooms . inc ( ) ;
262263
263- let config = extension ! ( req, Config ) ;
264264 let err = anyhow ! (
265265 "Failed to serve the rustdoc file '{}' because rewriting it surpassed the memory limit of {} bytes" ,
266266 file_path, config. max_parse_memory,
@@ -278,13 +278,21 @@ impl RustdocPage {
278278 . headers
279279 . set ( CacheControl ( vec ! [ CacheDirective :: MaxAge ( 0 ) ] ) ) ;
280280 } 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 (
283284 "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+ }
288296 }
289297 Ok ( response)
290298 }
@@ -895,13 +903,17 @@ mod test {
895903 #[ test]
896904 fn cache_headers ( ) {
897905 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+
898911 env. fake_release ( )
899912 . name ( "dummy" )
900913 . version ( "0.1.0" )
901914 . archive_storage ( true )
902915 . rustdoc_file ( "dummy/index.html" )
903916 . create ( ) ?;
904-
905917 let resp = env. frontend ( ) . get ( "/dummy/latest/dummy/" ) . send ( ) ?;
906918 assert_eq ! ( resp. headers( ) . get( "Cache-Control" ) . unwrap( ) , & "max-age=0" ) ;
907919
0 commit comments