-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
reactrouterv6.tsx
336 lines (289 loc) · 11.5 KB
/
reactrouterv6.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Inspired from Donnie McNeal's solution:
// https://gist.github.com/wontondon/e8c4bdf2888875e4c755712e99279536
import { WINDOW } from '@sentry/browser';
import { SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core';
import type { Transaction, TransactionContext, TransactionSource } from '@sentry/types';
import { getNumberOfUrlSegments, logger } from '@sentry/utils';
import hoistNonReactStatics from 'hoist-non-react-statics';
import * as React from 'react';
import { DEBUG_BUILD } from './debug-build';
import type {
Action,
AgnosticDataRouteMatch,
CreateRouterFunction,
CreateRoutesFromChildren,
Location,
MatchRoutes,
RouteMatch,
RouteObject,
Router,
RouterState,
UseEffect,
UseLocation,
UseNavigationType,
UseRoutes,
} from './types';
let activeTransaction: Transaction | undefined;
let _useEffect: UseEffect;
let _useLocation: UseLocation;
let _useNavigationType: UseNavigationType;
let _createRoutesFromChildren: CreateRoutesFromChildren;
let _matchRoutes: MatchRoutes;
let _customStartTransaction: (context: TransactionContext) => Transaction | undefined;
let _startTransactionOnLocationChange: boolean;
let _stripBasename: boolean = false;
const SENTRY_TAGS = {
'routing.instrumentation': 'react-router-v6',
};
export function reactRouterV6Instrumentation(
useEffect: UseEffect,
useLocation: UseLocation,
useNavigationType: UseNavigationType,
createRoutesFromChildren: CreateRoutesFromChildren,
matchRoutes: MatchRoutes,
stripBasename?: boolean,
) {
return (
customStartTransaction: (context: TransactionContext) => Transaction | undefined,
startTransactionOnPageLoad = true,
startTransactionOnLocationChange = true,
): void => {
const initPathName = WINDOW && WINDOW.location && WINDOW.location.pathname;
if (startTransactionOnPageLoad && initPathName) {
activeTransaction = customStartTransaction({
name: initPathName,
op: 'pageload',
origin: 'auto.pageload.react.reactrouterv6',
tags: SENTRY_TAGS,
metadata: {
source: 'url',
},
});
}
_useEffect = useEffect;
_useLocation = useLocation;
_useNavigationType = useNavigationType;
_matchRoutes = matchRoutes;
_createRoutesFromChildren = createRoutesFromChildren;
_stripBasename = stripBasename || false;
_customStartTransaction = customStartTransaction;
_startTransactionOnLocationChange = startTransactionOnLocationChange;
};
}
/**
* Strip the basename from a pathname if exists.
*
* Vendored and modified from `react-router`
* https://github.com/remix-run/react-router/blob/462bb712156a3f739d6139a0f14810b76b002df6/packages/router/utils.ts#L1038
*/
function stripBasenameFromPathname(pathname: string, basename: string): string {
if (!basename || basename === '/') {
return pathname;
}
if (!pathname.toLowerCase().startsWith(basename.toLowerCase())) {
return pathname;
}
// We want to leave trailing slash behavior in the user's control, so if they
// specify a basename with a trailing slash, we should support it
const startIndex = basename.endsWith('/') ? basename.length - 1 : basename.length;
const nextChar = pathname.charAt(startIndex);
if (nextChar && nextChar !== '/') {
// pathname does not start with basename/
return pathname;
}
return pathname.slice(startIndex) || '/';
}
function getNormalizedName(
routes: RouteObject[],
location: Location,
branches: RouteMatch[],
basename: string = '',
): [string, TransactionSource] {
if (!routes || routes.length === 0) {
return [_stripBasename ? stripBasenameFromPathname(location.pathname, basename) : location.pathname, 'url'];
}
let pathBuilder = '';
if (branches) {
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let x = 0; x < branches.length; x++) {
const branch = branches[x];
const route = branch.route;
if (route) {
// Early return if index route
if (route.index) {
return [_stripBasename ? stripBasenameFromPathname(branch.pathname, basename) : branch.pathname, 'route'];
}
const path = route.path;
if (path) {
const newPath = path[0] === '/' || pathBuilder[pathBuilder.length - 1] === '/' ? path : `/${path}`;
pathBuilder += newPath;
if (basename + branch.pathname === location.pathname) {
if (
// If the route defined on the element is something like
// <Route path="/stores/:storeId/products/:productId" element={<div>Product</div>} />
// We should check against the branch.pathname for the number of / seperators
getNumberOfUrlSegments(pathBuilder) !== getNumberOfUrlSegments(branch.pathname) &&
// We should not count wildcard operators in the url segments calculation
pathBuilder.slice(-2) !== '/*'
) {
return [(_stripBasename ? '' : basename) + newPath, 'route'];
}
return [(_stripBasename ? '' : basename) + pathBuilder, 'route'];
}
}
}
}
}
return [_stripBasename ? stripBasenameFromPathname(location.pathname, basename) : location.pathname, 'url'];
}
function updatePageloadTransaction(
location: Location,
routes: RouteObject[],
matches?: AgnosticDataRouteMatch,
basename?: string,
): void {
const branches = Array.isArray(matches)
? matches
: (_matchRoutes(routes, location, basename) as unknown as RouteMatch[]);
if (activeTransaction && branches) {
const [name, source] = getNormalizedName(routes, location, branches, basename);
activeTransaction.updateName(name);
activeTransaction.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, source);
}
}
function handleNavigation(
location: Location,
routes: RouteObject[],
navigationType: Action,
matches?: AgnosticDataRouteMatch,
basename?: string,
): void {
const branches = Array.isArray(matches) ? matches : _matchRoutes(routes, location, basename);
if (_startTransactionOnLocationChange && (navigationType === 'PUSH' || navigationType === 'POP') && branches) {
if (activeTransaction) {
activeTransaction.end();
}
const [name, source] = getNormalizedName(routes, location, branches, basename);
activeTransaction = _customStartTransaction({
name,
op: 'navigation',
origin: 'auto.navigation.react.reactrouterv6',
tags: SENTRY_TAGS,
metadata: {
source,
},
});
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function withSentryReactRouterV6Routing<P extends Record<string, any>, R extends React.FC<P>>(Routes: R): R {
if (
!_useEffect ||
!_useLocation ||
!_useNavigationType ||
!_createRoutesFromChildren ||
!_matchRoutes ||
!_customStartTransaction
) {
DEBUG_BUILD &&
logger.warn(`reactRouterV6Instrumentation was unable to wrap Routes because of one or more missing parameters.
useEffect: ${_useEffect}. useLocation: ${_useLocation}. useNavigationType: ${_useNavigationType}.
createRoutesFromChildren: ${_createRoutesFromChildren}. matchRoutes: ${_matchRoutes}. customStartTransaction: ${_customStartTransaction}.`);
return Routes;
}
let isMountRenderPass: boolean = true;
const SentryRoutes: React.FC<P> = (props: P) => {
const location = _useLocation();
const navigationType = _useNavigationType();
_useEffect(
() => {
const routes = _createRoutesFromChildren(props.children) as RouteObject[];
if (isMountRenderPass) {
updatePageloadTransaction(location, routes);
isMountRenderPass = false;
} else {
handleNavigation(location, routes, navigationType);
}
},
// `props.children` is purpusely not included in the dependency array, because we do not want to re-run this effect
// when the children change. We only want to start transactions when the location or navigation type change.
[location, navigationType],
);
// @ts-expect-error Setting more specific React Component typing for `R` generic above
// will break advanced type inference done by react router params
return <Routes {...props} />;
};
hoistNonReactStatics(SentryRoutes, Routes);
// @ts-expect-error Setting more specific React Component typing for `R` generic above
// will break advanced type inference done by react router params
return SentryRoutes;
}
export function wrapUseRoutes(origUseRoutes: UseRoutes): UseRoutes {
if (!_useEffect || !_useLocation || !_useNavigationType || !_matchRoutes || !_customStartTransaction) {
DEBUG_BUILD &&
logger.warn(
'reactRouterV6Instrumentation was unable to wrap `useRoutes` because of one or more missing parameters.',
);
return origUseRoutes;
}
let isMountRenderPass: boolean = true;
const SentryRoutes: React.FC<{
children?: React.ReactNode;
routes: RouteObject[];
locationArg?: Partial<Location> | string;
}> = (props: { children?: React.ReactNode; routes: RouteObject[]; locationArg?: Partial<Location> | string }) => {
const { routes, locationArg } = props;
const Routes = origUseRoutes(routes, locationArg);
const location = _useLocation();
const navigationType = _useNavigationType();
// A value with stable identity to either pick `locationArg` if available or `location` if not
const stableLocationParam =
typeof locationArg === 'string' || (locationArg && locationArg.pathname)
? (locationArg as { pathname: string })
: location;
_useEffect(() => {
const normalizedLocation =
typeof stableLocationParam === 'string' ? { pathname: stableLocationParam } : stableLocationParam;
if (isMountRenderPass) {
updatePageloadTransaction(normalizedLocation, routes);
isMountRenderPass = false;
} else {
handleNavigation(normalizedLocation, routes, navigationType);
}
}, [navigationType, stableLocationParam]);
return Routes;
};
// eslint-disable-next-line react/display-name
return (routes: RouteObject[], locationArg?: Partial<Location> | string): React.ReactElement | null => {
return <SentryRoutes routes={routes} locationArg={locationArg} />;
};
}
export function wrapCreateBrowserRouter<
TState extends RouterState = RouterState,
TRouter extends Router<TState> = Router<TState>,
>(createRouterFunction: CreateRouterFunction<TState, TRouter>): CreateRouterFunction<TState, TRouter> {
// `opts` for createBrowserHistory and createMemoryHistory are different, but also not relevant for us at the moment.
// `basename` is the only option that is relevant for us, and it is the same for all.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return function (routes: RouteObject[], opts?: Record<string, any> & { basename?: string }): TRouter {
const router = createRouterFunction(routes, opts);
const basename = opts && opts.basename;
// The initial load ends when `createBrowserRouter` is called.
// This is the earliest convenient time to update the transaction name.
// Callbacks to `router.subscribe` are not called for the initial load.
if (router.state.historyAction === 'POP' && activeTransaction) {
updatePageloadTransaction(router.state.location, routes, undefined, basename);
}
router.subscribe((state: RouterState) => {
const location = state.location;
if (
_startTransactionOnLocationChange &&
(state.historyAction === 'PUSH' || state.historyAction === 'POP') &&
activeTransaction
) {
handleNavigation(location, routes, state.historyAction, undefined, basename);
}
});
return router;
};
}