11import { debug } from '@sentry/core' ;
22import { DEBUG_BUILD } from '../debug-build' ;
33
4+ /**
5+ * Strip the basename from a pathname if exists.
6+ *
7+ * Vendored and modified from `react-router`
8+ * https://github.com/remix-run/react-router/blob/462bb712156a3f739d6139a0f14810b76b002df6/packages/router/utils.ts#L1038
9+ */
10+ export function stripBasenameFromPathname ( pathname : string , basename : string ) : string {
11+ if ( ! basename || basename === '/' ) {
12+ return pathname ;
13+ }
14+
15+ if ( ! pathname . toLowerCase ( ) . startsWith ( basename . toLowerCase ( ) ) ) {
16+ return pathname ;
17+ }
18+
19+ // We want to leave trailing slash behavior in the user's control, so if they
20+ // specify a basename with a trailing slash, we should support it
21+ const startIndex = basename . endsWith ( '/' ) ? basename . length - 1 : basename . length ;
22+ const nextChar = pathname . charAt ( startIndex ) ;
23+ if ( nextChar && nextChar !== '/' ) {
24+ // pathname does not start with basename/
25+ return pathname ;
26+ }
27+
28+ return pathname . slice ( startIndex ) || '/' ;
29+ }
30+
431// Cache for sorted manifests - keyed by manifest array reference
532const SORTED_MANIFEST_CACHE = new WeakMap < string [ ] , string [ ] > ( ) ;
633
@@ -13,17 +40,7 @@ export function matchRouteManifest(pathname: string, manifest: string[], basenam
1340 return null ;
1441 }
1542
16- let normalizedPathname = pathname ;
17- if ( basename && basename !== '/' ) {
18- const base = basename . endsWith ( '/' ) ? basename . slice ( 0 , - 1 ) : basename ;
19- if ( normalizedPathname . toLowerCase ( ) . startsWith ( base . toLowerCase ( ) ) ) {
20- // Verify basename ends at segment boundary (followed by / or end of string)
21- const nextChar = normalizedPathname . charAt ( base . length ) ;
22- if ( nextChar === '/' || nextChar === '' ) {
23- normalizedPathname = normalizedPathname . slice ( base . length ) || '/' ;
24- }
25- }
26- }
43+ const normalizedPathname = basename ? stripBasenameFromPathname ( pathname , basename ) : pathname ;
2744
2845 let sorted = SORTED_MANIFEST_CACHE . get ( manifest ) ;
2946 if ( ! sorted ) {
0 commit comments