11// Inspired from Donnie McNeal's solution:
22// https://gist.github.com/wontondon/e8c4bdf2888875e4c755712e99279536
33
4- import { Transaction , TransactionContext } from '@sentry/types' ;
4+ import { Transaction , TransactionContext , TransactionSource } from '@sentry/types' ;
55import { getGlobalObject , logger } from '@sentry/utils' ;
66import hoistNonReactStatics from 'hoist-non-react-statics' ;
77import React from 'react' ;
@@ -48,14 +48,6 @@ const SENTRY_TAGS = {
4848 'routing.instrumentation' : 'react-router-v6' ,
4949} ;
5050
51- function getInitPathName ( ) : string | undefined {
52- if ( global && global . location ) {
53- return global . location . pathname ;
54- }
55-
56- return undefined ;
57- }
58-
5951export function reactRouterV6Instrumentation (
6052 useEffect : UseEffect ,
6153 useLocation : UseLocation ,
@@ -68,12 +60,15 @@ export function reactRouterV6Instrumentation(
6860 startTransactionOnPageLoad = true ,
6961 startTransactionOnLocationChange = true ,
7062 ) : void => {
71- const initPathName = getInitPathName ( ) ;
63+ const initPathName = global && global . location && global . location . pathname ;
7264 if ( startTransactionOnPageLoad && initPathName ) {
7365 activeTransaction = customStartTransaction ( {
7466 name : initPathName ,
7567 op : 'pageload' ,
7668 tags : SENTRY_TAGS ,
69+ metadata : {
70+ source : 'url' ,
71+ } ,
7772 } ) ;
7873 }
7974
@@ -88,9 +83,13 @@ export function reactRouterV6Instrumentation(
8883 } ;
8984}
9085
91- const getTransactionName = ( routes : RouteObject [ ] , location : Location , matchRoutes : MatchRoutes ) : string => {
86+ function getNormalizedName (
87+ routes : RouteObject [ ] ,
88+ location : Location ,
89+ matchRoutes : MatchRoutes ,
90+ ) : [ string , TransactionSource ] {
9291 if ( ! routes || routes . length === 0 || ! matchRoutes ) {
93- return location . pathname ;
92+ return [ location . pathname , 'url' ] ;
9493 }
9594
9695 const branches = matchRoutes ( routes , location ) ;
@@ -99,13 +98,16 @@ const getTransactionName = (routes: RouteObject[], location: Location, matchRout
9998 // eslint-disable-next-line @typescript-eslint/prefer-for-of
10099 for ( let x = 0 ; x < branches . length ; x ++ ) {
101100 if ( branches [ x ] . route && branches [ x ] . route . path && branches [ x ] . pathname === location . pathname ) {
102- return branches [ x ] . route . path || location . pathname ;
101+ const path = branches [ x ] . route . path ;
102+ if ( path ) {
103+ return [ path , 'route' ] ;
104+ }
103105 }
104106 }
105107 }
106108
107- return location . pathname ;
108- } ;
109+ return [ location . pathname , 'url' ] ;
110+ }
109111
110112export function withSentryReactRouterV6Routing < P extends Record < string , any > , R extends React . FC < P > > ( Routes : R ) : R {
111113 if (
@@ -136,7 +138,9 @@ export function withSentryReactRouterV6Routing<P extends Record<string, any>, R
136138 isBaseLocation = true ;
137139
138140 if ( activeTransaction ) {
139- activeTransaction . setName ( getTransactionName ( routes , location , _matchRoutes ) ) ;
141+ const [ name , source ] = getNormalizedName ( routes , location , _matchRoutes ) ;
142+ activeTransaction . setName ( name ) ;
143+ activeTransaction . setMetadata ( { source } ) ;
140144 }
141145
142146 // eslint-disable-next-line react-hooks/exhaustive-deps
@@ -156,10 +160,14 @@ export function withSentryReactRouterV6Routing<P extends Record<string, any>, R
156160 activeTransaction . finish ( ) ;
157161 }
158162
163+ const [ name , source ] = getNormalizedName ( routes , location , _matchRoutes ) ;
159164 activeTransaction = _customStartTransaction ( {
160- name : getTransactionName ( routes , location , _matchRoutes ) ,
165+ name,
161166 op : 'navigation' ,
162167 tags : SENTRY_TAGS ,
168+ metadata : {
169+ source,
170+ } ,
163171 } ) ;
164172 }
165173 } , [ props . children , location , navigationType , isBaseLocation ] ) ;
0 commit comments