@@ -3,7 +3,7 @@ use super::{
3
3
} ;
4
4
use askama:: Template ;
5
5
use axum:: {
6
- Router as AxumRouter ,
6
+ Extension , Router as AxumRouter ,
7
7
extract:: Request as AxumHttpRequest ,
8
8
handler:: Handler as AxumHandler ,
9
9
middleware:: { self , Next } ,
@@ -102,6 +102,13 @@ pub(super) fn build_metric_routes() -> AxumRouter {
102
102
)
103
103
}
104
104
105
+ fn cached_permanent_redirect ( uri : & str ) -> impl IntoResponse {
106
+ (
107
+ Extension ( CachePolicy :: ForeverInCdnAndBrowser ) ,
108
+ Redirect :: permanent ( uri) ,
109
+ )
110
+ }
111
+
105
112
pub ( super ) fn build_axum_routes ( ) -> AxumRouter {
106
113
// hint for naming axum routes:
107
114
// when routes overlap, the route parameters at the same position
@@ -123,18 +130,18 @@ pub(super) fn build_axum_routes() -> AxumRouter {
123
130
// https://support.google.com/webmasters/answer/183668?hl=en
124
131
. route (
125
132
"/robots.txt" ,
126
- get_static ( || async { Redirect :: permanent ( "/-/static/robots.txt" ) } ) ,
133
+ get_static ( || async { cached_permanent_redirect ( "/-/static/robots.txt" ) } ) ,
127
134
)
128
135
. route (
129
136
"/favicon.ico" ,
130
- get_static ( || async { Redirect :: permanent ( "/-/static/favicon.ico" ) } ) ,
137
+ get_static ( || async { cached_permanent_redirect ( "/-/static/favicon.ico" ) } ) ,
131
138
)
132
139
// `.nest` with fallbacks is currently broken, `.nest_service works
133
140
// https://github.com/tokio-rs/axum/issues/3138
134
141
. nest_service ( "/-/static" , build_static_router ( ) )
135
142
. route (
136
143
"/opensearch.xml" ,
137
- get_static ( || async { Redirect :: permanent ( "/-/static/opensearch.xml" ) } ) ,
144
+ get_static ( || async { cached_permanent_redirect ( "/-/static/opensearch.xml" ) } ) ,
138
145
)
139
146
. route_with_tsr (
140
147
"/sitemap.xml" ,
@@ -386,18 +393,34 @@ mod tests {
386
393
fn test_root_redirects ( ) {
387
394
async_wrapper ( |env| async move {
388
395
let web = env. web_app ( ) . await ;
396
+ let config = env. config ( ) ;
389
397
// These are "well-known" resources that will be requested from the root, but support
390
398
// 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 ?;
395
413
396
414
// This has previously been served with a url pointing to the root, it may be
397
415
// plausible to remove the redirects in the future, but for now we need to keep serving
398
416
// 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 ?;
401
424
402
425
Ok ( ( ) )
403
426
} ) ;
0 commit comments