@@ -11,22 +11,26 @@ export default async (request: Request, context) => {
11
11
const redirectableSections = [ '/' , '/about' , '/blog' , '/get-involved' , '/news' , '/search' ] ;
12
12
const versionPath = versionMatch [ 0 ] ; // ex /v1.15
13
13
const remainder = pathname . slice ( versionPath . length ) || '/' ;
14
- // Check if the remainder starts with a language code
15
- const languageCodeMatch = remainder . match ( / ^ \/ ( [ a - z ] { 2 } ) \/ / ) ;
14
+ // Check if the remainder starts with an optional leading slash followed by a language code
15
+ const languageCodeMatch = remainder . match ( / ^ \/ ? ( [ a - z ] { 2 } ) ( \/ | $ ) / ) ;
16
16
let languageCode : string | null = null ;
17
- let sectionPath : string = remainder ;
17
+ let remainingPathAfterLang = remainder ;
18
18
19
19
if ( languageCodeMatch ) {
20
20
languageCode = languageCodeMatch [ 1 ] ;
21
- sectionPath = remainder . slice ( languageCodeMatch [ 0 ] . length ) || '/' ; // slice to remove the leading slash of the language code
21
+ remainingPathAfterLang = remainder . slice ( languageCodeMatch [ 0 ] . length ) ;
22
22
}
23
- // Check if the base section (after removing language code) is redirectable
23
+ // If there's a language code and no further path, redirect to the homepage with the language code
24
+ if ( languageCode && remainingPathAfterLang === '' ) {
25
+ return Response . redirect ( new URL ( `/latest/${ languageCode } /` , url . origin ) , 301 ) ;
26
+ }
27
+ // Check if the remaining path (after version and optional language code) starts with a redirectable section
24
28
const shouldRedirect = redirectableSections . some ( section =>
25
- sectionPath === section || sectionPath . startsWith ( `${ section } /` ) || ( section === '/' && sectionPath === '' ) ) ;
29
+ remainingPathAfterLang === section || remainingPathAfterLang . startsWith ( `${ section } /` ) || ( section === '/' && remainingPathAfterLang === '' ) ) ;
26
30
27
31
if ( shouldRedirect ) {
28
- const cleanSectionPath = sectionPath . startsWith ( '/' ) ? sectionPath . substring ( 1 ) : sectionPath ;
29
- const newPath = `/latest/${ cleanSectionPath } ` ; // English path doesn't include language code
32
+ const cleanRemainderPath = remainingPathAfterLang . startsWith ( '/' ) ? remainingPathAfterLang . substring ( 1 ) : remainingPathAfterLang ;
33
+ const newPath = `/latest/${ languageCode ? ` ${ languageCode } /` : '' } ${ cleanRemainderPath } ` ;
30
34
return Response . redirect ( new URL ( newPath , url . origin ) , 301 ) ;
31
35
}
32
36
// For version-specific paths we want to keep, tell Netlify to stop processing this function
0 commit comments