@@ -35,6 +35,7 @@ let _createRoutesFromChildren: CreateRoutesFromChildren;
3535let _matchRoutes : MatchRoutes ;
3636let _customStartTransaction : ( context : TransactionContext ) => Transaction | undefined ;
3737let _startTransactionOnLocationChange : boolean ;
38+ let _stripBasename : boolean = false ;
3839
3940const SENTRY_TAGS = {
4041 'routing.instrumentation' : 'react-router-v6' ,
@@ -46,6 +47,7 @@ export function reactRouterV6Instrumentation(
4647 useNavigationType : UseNavigationType ,
4748 createRoutesFromChildren : CreateRoutesFromChildren ,
4849 matchRoutes : MatchRoutes ,
50+ stripBasename ?: boolean ,
4951) {
5052 return (
5153 customStartTransaction : ( context : TransactionContext ) => Transaction | undefined ,
@@ -70,20 +72,48 @@ export function reactRouterV6Instrumentation(
7072 _useNavigationType = useNavigationType ;
7173 _matchRoutes = matchRoutes ;
7274 _createRoutesFromChildren = createRoutesFromChildren ;
75+ _stripBasename = stripBasename || false ;
7376
7477 _customStartTransaction = customStartTransaction ;
7578 _startTransactionOnLocationChange = startTransactionOnLocationChange ;
7679 } ;
7780}
7881
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+
79109function getNormalizedName (
80110 routes : RouteObject [ ] ,
81111 location : Location ,
82112 branches : RouteMatch [ ] ,
83113 basename : string = '' ,
84114) : [ string , TransactionSource ] {
85115 if ( ! routes || routes . length === 0 ) {
86- return [ location . pathname , 'url' ] ;
116+ return [ _stripBasename ? stripBasenameFromPathname ( location . pathname , basename ) : location . pathname , 'url' ] ;
87117 }
88118
89119 let pathBuilder = '' ;
@@ -95,7 +125,7 @@ function getNormalizedName(
95125 if ( route ) {
96126 // Early return if index route
97127 if ( route . index ) {
98- return [ branch . pathname , 'route' ] ;
128+ return [ _stripBasename ? stripBasenameFromPathname ( branch . pathname , basename ) : branch . pathname , 'route' ] ;
99129 }
100130
101131 const path = route . path ;
@@ -112,16 +142,16 @@ function getNormalizedName(
112142 // We should not count wildcard operators in the url segments calculation
113143 pathBuilder . slice ( - 2 ) !== '/*'
114144 ) {
115- return [ basename + newPath , 'route' ] ;
145+ return [ ( _stripBasename ? '' : basename ) + newPath , 'route' ] ;
116146 }
117- return [ basename + pathBuilder , 'route' ] ;
147+ return [ ( _stripBasename ? '' : basename ) + pathBuilder , 'route' ] ;
118148 }
119149 }
120150 }
121151 }
122152 }
123153
124- return [ location . pathname , 'url' ] ;
154+ return [ _stripBasename ? stripBasenameFromPathname ( location . pathname , basename ) : location . pathname , 'url' ] ;
125155}
126156
127157function updatePageloadTransaction (
0 commit comments