@@ -3,7 +3,7 @@ use super::{
33} ;
44use askama:: Template ;
55use axum:: {
6- Router as AxumRouter ,
6+ Extension , Router as AxumRouter ,
77 extract:: Request as AxumHttpRequest ,
88 handler:: Handler as AxumHandler ,
99 middleware:: { self , Next } ,
@@ -102,6 +102,13 @@ pub(super) fn build_metric_routes() -> AxumRouter {
102102 )
103103}
104104
105+ fn cached_permanent_redirect ( uri : & str ) -> impl IntoResponse {
106+ (
107+ Extension ( CachePolicy :: ForeverInCdnAndBrowser ) ,
108+ Redirect :: permanent ( uri) ,
109+ )
110+ }
111+
105112pub ( super ) fn build_axum_routes ( ) -> AxumRouter {
106113 // hint for naming axum routes:
107114 // when routes overlap, the route parameters at the same position
@@ -123,18 +130,18 @@ pub(super) fn build_axum_routes() -> AxumRouter {
123130 // https://support.google.com/webmasters/answer/183668?hl=en
124131 . route (
125132 "/robots.txt" ,
126- get_static ( || async { Redirect :: permanent ( "/-/static/robots.txt" ) } ) ,
133+ get_static ( || async { cached_permanent_redirect ( "/-/static/robots.txt" ) } ) ,
127134 )
128135 . route (
129136 "/favicon.ico" ,
130- get_static ( || async { Redirect :: permanent ( "/-/static/favicon.ico" ) } ) ,
137+ get_static ( || async { cached_permanent_redirect ( "/-/static/favicon.ico" ) } ) ,
131138 )
132139 // `.nest` with fallbacks is currently broken, `.nest_service works
133140 // https://github.com/tokio-rs/axum/issues/3138
134141 . nest_service ( "/-/static" , build_static_router ( ) )
135142 . route (
136143 "/opensearch.xml" ,
137- get_static ( || async { Redirect :: permanent ( "/-/static/opensearch.xml" ) } ) ,
144+ get_static ( || async { cached_permanent_redirect ( "/-/static/opensearch.xml" ) } ) ,
138145 )
139146 . route_with_tsr (
140147 "/sitemap.xml" ,
@@ -386,18 +393,34 @@ mod tests {
386393 fn test_root_redirects ( ) {
387394 async_wrapper ( |env| async move {
388395 let web = env. web_app ( ) . await ;
396+ let config = env. config ( ) ;
389397 // These are "well-known" resources that will be requested from the root, but support
390398 // redirection
391- web. assert_redirect ( "/favicon.ico" , "/-/static/favicon.ico" )
392- . await ?;
393- web. assert_redirect ( "/robots.txt" , "/-/static/robots.txt" )
394- . await ?;
399+ web. assert_redirect_cached (
400+ "/favicon.ico" ,
401+ "/-/static/favicon.ico" ,
402+ CachePolicy :: ForeverInCdnAndBrowser ,
403+ & config,
404+ )
405+ . await ?;
406+ web. assert_redirect_cached (
407+ "/robots.txt" ,
408+ "/-/static/robots.txt" ,
409+ CachePolicy :: ForeverInCdnAndBrowser ,
410+ & config,
411+ )
412+ . await ?;
395413
396414 // This has previously been served with a url pointing to the root, it may be
397415 // plausible to remove the redirects in the future, but for now we need to keep serving
398416 // it.
399- web. assert_redirect ( "/opensearch.xml" , "/-/static/opensearch.xml" )
400- . await ?;
417+ web. assert_redirect_cached (
418+ "/opensearch.xml" ,
419+ "/-/static/opensearch.xml" ,
420+ CachePolicy :: ForeverInCdnAndBrowser ,
421+ & config,
422+ )
423+ . await ?;
401424
402425 Ok ( ( ) )
403426 } ) ;
0 commit comments