1
1
// Inspired from Donnie McNeal's solution:
2
2
// https://gist.github.com/wontondon/e8c4bdf2888875e4c755712e99279536
3
3
4
- import { Transaction , TransactionContext } from '@sentry/types' ;
4
+ import { Transaction , TransactionContext , TransactionSource } from '@sentry/types' ;
5
5
import { getGlobalObject , logger } from '@sentry/utils' ;
6
6
import hoistNonReactStatics from 'hoist-non-react-statics' ;
7
7
import React from 'react' ;
@@ -48,14 +48,6 @@ const SENTRY_TAGS = {
48
48
'routing.instrumentation' : 'react-router-v6' ,
49
49
} ;
50
50
51
- function getInitPathName ( ) : string | undefined {
52
- if ( global && global . location ) {
53
- return global . location . pathname ;
54
- }
55
-
56
- return undefined ;
57
- }
58
-
59
51
export function reactRouterV6Instrumentation (
60
52
useEffect : UseEffect ,
61
53
useLocation : UseLocation ,
@@ -68,12 +60,15 @@ export function reactRouterV6Instrumentation(
68
60
startTransactionOnPageLoad = true ,
69
61
startTransactionOnLocationChange = true ,
70
62
) : void => {
71
- const initPathName = getInitPathName ( ) ;
63
+ const initPathName = global && global . location && global . location . pathname ;
72
64
if ( startTransactionOnPageLoad && initPathName ) {
73
65
activeTransaction = customStartTransaction ( {
74
66
name : initPathName ,
75
67
op : 'pageload' ,
76
68
tags : SENTRY_TAGS ,
69
+ metadata : {
70
+ source : 'url' ,
71
+ } ,
77
72
} ) ;
78
73
}
79
74
@@ -88,9 +83,13 @@ export function reactRouterV6Instrumentation(
88
83
} ;
89
84
}
90
85
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 ] {
92
91
if ( ! routes || routes . length === 0 || ! matchRoutes ) {
93
- return location . pathname ;
92
+ return [ location . pathname , 'url' ] ;
94
93
}
95
94
96
95
const branches = matchRoutes ( routes , location ) ;
@@ -99,13 +98,16 @@ const getTransactionName = (routes: RouteObject[], location: Location, matchRout
99
98
// eslint-disable-next-line @typescript-eslint/prefer-for-of
100
99
for ( let x = 0 ; x < branches . length ; x ++ ) {
101
100
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
+ }
103
105
}
104
106
}
105
107
}
106
108
107
- return location . pathname ;
108
- } ;
109
+ return [ location . pathname , 'url' ] ;
110
+ }
109
111
110
112
export function withSentryReactRouterV6Routing < P extends Record < string , any > , R extends React . FC < P > > ( Routes : R ) : R {
111
113
if (
@@ -136,7 +138,9 @@ export function withSentryReactRouterV6Routing<P extends Record<string, any>, R
136
138
isBaseLocation = true ;
137
139
138
140
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 } ) ;
140
144
}
141
145
142
146
// eslint-disable-next-line react-hooks/exhaustive-deps
@@ -156,10 +160,14 @@ export function withSentryReactRouterV6Routing<P extends Record<string, any>, R
156
160
activeTransaction . finish ( ) ;
157
161
}
158
162
163
+ const [ name , source ] = getNormalizedName ( routes , location , _matchRoutes ) ;
159
164
activeTransaction = _customStartTransaction ( {
160
- name : getTransactionName ( routes , location , _matchRoutes ) ,
165
+ name,
161
166
op : 'navigation' ,
162
167
tags : SENTRY_TAGS ,
168
+ metadata : {
169
+ source,
170
+ } ,
163
171
} ) ;
164
172
}
165
173
} , [ props . children , location , navigationType , isBaseLocation ] ) ;
0 commit comments