File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -4,6 +4,21 @@ const WP_ROOT = process.env.VITE_REACT_APP_WP || '/wp';
44// Escapes special regex metacharacters in a literal string.
55const escapeRegex = ( s ) => s . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) ;
66
7+ // Normalizes WP_ROOT to have exactly one leading "/" and no trailing "/".
8+ // Examples:
9+ // "wp" -> "/wp"
10+ // "/wp/" -> "/wp"
11+ // "//wp//"-> "/wp"
12+ // "/" -> "" (no subdirectory)
13+ const normalizeWpRoot = ( root ) => {
14+ if ( ! root ) return '' ;
15+ let r = root . trim ( ) ;
16+ // Remove all leading and trailing slashes
17+ r = r . replace ( / ^ \/ + / , '' ) . replace ( / \/ + $ / , '' ) ;
18+ if ( ! r ) return '' ;
19+ return '/' + r ;
20+ } ;
21+
722// Builds a regex that matches the scheme + configured WP hostname(s) + optional WP
823// subdirectory path (e.g. /wp), so the entire origin prefix can be replaced with the
924// locale slug in one step.
@@ -13,7 +28,7 @@ const buildUrlRegex = () => {
1328 ?. split ( "," ) . map ( h => h . trim ( ) ) . filter ( Boolean ) || [ ] ;
1429 if ( ! hosts . length ) return null ;
1530 const hostsPattern = hosts . map ( escapeRegex ) . join ( '|' ) ;
16- const wpRootPattern = escapeRegex ( WP_ROOT ) ;
31+ const wpRootPattern = escapeRegex ( normalizeWpRoot ( WP_ROOT ) ) ;
1732 return new RegExp ( `^(http|https)://(${ hostsPattern } )(?:${ wpRootPattern } (?=/|$))?` , 'ig' ) ;
1833} ;
1934
You can’t perform that action at this time.
0 commit comments