@@ -235,13 +235,13 @@ impl RustdocPage {
235
235
let is_latest_url = self . is_latest_url ;
236
236
// Build the page of documentation
237
237
let ctx = ctry ! ( req, tera:: Context :: from_serialize( self ) ) ;
238
+ let config = extension ! ( req, Config ) ;
238
239
// Extract the head and body of the rustdoc file so that we can insert it into our own html
239
240
// while logging OOM errors from html rewriting
240
241
let html = match utils:: rewrite_lol ( rustdoc_html, max_parse_memory, ctx, templates) {
241
242
Err ( RewritingError :: MemoryLimitExceeded ( ..) ) => {
242
243
metrics. html_rewrite_ooms . inc ( ) ;
243
244
244
- let config = extension ! ( req, Config ) ;
245
245
let err = anyhow ! (
246
246
"Failed to serve the rustdoc file '{}' because rewriting it surpassed the memory limit of {} bytes" ,
247
247
file_path, config. max_parse_memory,
@@ -259,13 +259,21 @@ impl RustdocPage {
259
259
. headers
260
260
. set ( CacheControl ( vec ! [ CacheDirective :: MaxAge ( 0 ) ] ) ) ;
261
261
} 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 (
264
265
"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
+ }
269
277
}
270
278
Ok ( response)
271
279
}
@@ -864,13 +872,17 @@ mod test {
864
872
#[ test]
865
873
fn cache_headers ( ) {
866
874
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
+
867
880
env. fake_release ( )
868
881
. name ( "dummy" )
869
882
. version ( "0.1.0" )
870
883
. archive_storage ( true )
871
884
. rustdoc_file ( "dummy/index.html" )
872
885
. create ( ) ?;
873
-
874
886
let resp = env. frontend ( ) . get ( "/dummy/latest/dummy/" ) . send ( ) ?;
875
887
assert_eq ! ( resp. headers( ) . get( "Cache-Control" ) . unwrap( ) , & "max-age=0" ) ;
876
888
0 commit comments