@@ -35,6 +35,7 @@ let _createRoutesFromChildren: CreateRoutesFromChildren;
35
35
let _matchRoutes : MatchRoutes ;
36
36
let _customStartTransaction : ( context : TransactionContext ) => Transaction | undefined ;
37
37
let _startTransactionOnLocationChange : boolean ;
38
+ let _stripBasename : boolean = false ;
38
39
39
40
const SENTRY_TAGS = {
40
41
'routing.instrumentation' : 'react-router-v6' ,
@@ -46,6 +47,7 @@ export function reactRouterV6Instrumentation(
46
47
useNavigationType : UseNavigationType ,
47
48
createRoutesFromChildren : CreateRoutesFromChildren ,
48
49
matchRoutes : MatchRoutes ,
50
+ stripBasename ?: boolean ,
49
51
) {
50
52
return (
51
53
customStartTransaction : ( context : TransactionContext ) => Transaction | undefined ,
@@ -70,20 +72,48 @@ export function reactRouterV6Instrumentation(
70
72
_useNavigationType = useNavigationType ;
71
73
_matchRoutes = matchRoutes ;
72
74
_createRoutesFromChildren = createRoutesFromChildren ;
75
+ _stripBasename = stripBasename || false ;
73
76
74
77
_customStartTransaction = customStartTransaction ;
75
78
_startTransactionOnLocationChange = startTransactionOnLocationChange ;
76
79
} ;
77
80
}
78
81
82
+ /**
83
+ * Strip the basename from a pathname if exists.
84
+ *
85
+ * Vendored and modified from `react-router`
86
+ * https://github.com/remix-run/react-router/blob/462bb712156a3f739d6139a0f14810b76b002df6/packages/router/utils.ts#L1038
87
+ */
88
+ function stripBasenameFromPathname ( pathname : string , basename : string ) : string {
89
+ if ( ! basename || basename === '/' ) {
90
+ return pathname ;
91
+ }
92
+
93
+ if ( ! pathname . toLowerCase ( ) . startsWith ( basename . toLowerCase ( ) ) ) {
94
+ return pathname ;
95
+ }
96
+
97
+ // We want to leave trailing slash behavior in the user's control, so if they
98
+ // specify a basename with a trailing slash, we should support it
99
+ const startIndex = basename . endsWith ( '/' ) ? basename . length - 1 : basename . length ;
100
+ const nextChar = pathname . charAt ( startIndex ) ;
101
+ if ( nextChar && nextChar !== '/' ) {
102
+ // pathname does not start with basename/
103
+ return pathname ;
104
+ }
105
+
106
+ return pathname . slice ( startIndex ) || '/' ;
107
+ }
108
+
79
109
function getNormalizedName (
80
110
routes : RouteObject [ ] ,
81
111
location : Location ,
82
112
branches : RouteMatch [ ] ,
83
113
basename : string = '' ,
84
114
) : [ string , TransactionSource ] {
85
115
if ( ! routes || routes . length === 0 ) {
86
- return [ location . pathname , 'url' ] ;
116
+ return [ _stripBasename ? stripBasenameFromPathname ( location . pathname , basename ) : location . pathname , 'url' ] ;
87
117
}
88
118
89
119
let pathBuilder = '' ;
@@ -95,7 +125,7 @@ function getNormalizedName(
95
125
if ( route ) {
96
126
// Early return if index route
97
127
if ( route . index ) {
98
- return [ branch . pathname , 'route' ] ;
128
+ return [ _stripBasename ? stripBasenameFromPathname ( branch . pathname , basename ) : branch . pathname , 'route' ] ;
99
129
}
100
130
101
131
const path = route . path ;
@@ -112,16 +142,16 @@ function getNormalizedName(
112
142
// We should not count wildcard operators in the url segments calculation
113
143
pathBuilder . slice ( - 2 ) !== '/*'
114
144
) {
115
- return [ basename + newPath , 'route' ] ;
145
+ return [ ( _stripBasename ? '' : basename ) + newPath , 'route' ] ;
116
146
}
117
- return [ basename + pathBuilder , 'route' ] ;
147
+ return [ ( _stripBasename ? '' : basename ) + pathBuilder , 'route' ] ;
118
148
}
119
149
}
120
150
}
121
151
}
122
152
}
123
153
124
- return [ location . pathname , 'url' ] ;
154
+ return [ _stripBasename ? stripBasenameFromPathname ( location . pathname , basename ) : location . pathname , 'url' ] ;
125
155
}
126
156
127
157
function updatePageloadTransaction (
0 commit comments