Skip to content

Commit c3fb508

Browse files
committed
Switch back to the original implementation.
1 parent 1d42620 commit c3fb508

File tree

2 files changed

+54
-106
lines changed

2 files changed

+54
-106
lines changed

packages/react/src/reactrouterv6.tsx

Lines changed: 50 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ export function reactRouterV6BrowserTracingIntegration(
9898
integration.afterAllSetup(client);
9999

100100
const initPathName = WINDOW && WINDOW.location && WINDOW.location.pathname;
101-
102101
if (instrumentPageLoad && initPathName) {
103102
startBrowserTracingPageLoadSpan(client, {
104103
name: initPathName,
@@ -159,16 +158,16 @@ function sendIndexPath(pathBuilder: string, pathname: string, basename: string):
159158
return [formattedPath, 'route'];
160159
}
161160

162-
function matchHasWildcard(match: RouteMatch<string>): boolean {
163-
return !!match.params['*'];
164-
}
165-
166161
function pathEndsWithWildcard(path: string): boolean {
167162
return path.endsWith('*');
168163
}
169164

170-
function matchIsWildcardAndHasChildren(path: string, match: RouteMatch<string>): boolean {
171-
return (matchHasWildcard(match) && match.route.children && match.route.children.length > 0) || false;
165+
function pathIsWildcardAndHasChildren(path: string, branch: RouteMatch<string>): boolean {
166+
return (pathEndsWithWildcard(path) && branch.route.children && branch.route.children.length > 0) || false;
167+
}
168+
169+
function pathIsWildcardWithNoChildren(path: string, branch: RouteMatch<string>): boolean {
170+
return (pathEndsWithWildcard(path) && (!branch.route.children || branch.route.children.length === 0)) || false;
172171
}
173172

174173
function getNormalizedName(
@@ -179,31 +178,24 @@ function getNormalizedName(
179178
allRoutes: RouteObject[] = routes,
180179
): [string, TransactionSource] {
181180
if (!routes || routes.length === 0) {
182-
debugger;
183181
return [_stripBasename ? stripBasenameFromPathname(location.pathname, basename) : location.pathname, 'url'];
184182
}
185183

186-
const matchedRoutes = _matchRoutes(allRoutes, location, basename);
184+
const matchedRoutes = _matchRoutes(routes, location);
187185

188186
if (matchedRoutes) {
189-
const wildCardRoutes: RouteMatch[] = matchedRoutes.filter((match: RouteMatch) => {
190-
return matchHasWildcard(match);
191-
});
187+
const wildCardRoutes: RouteMatch[] = matchedRoutes.filter(
188+
(match: RouteMatch) => match.route.path && pathIsWildcardWithNoChildren(match.route.path, match),
189+
);
192190

193191
for (const wildCardRoute of wildCardRoutes) {
194192
const wildCardRouteMatch = _matchRoutes(allRoutes, location, wildCardRoute.pathnameBase);
195193

196194
if (wildCardRouteMatch) {
197-
const [name, source] = getNormalizedName(
198-
wildCardRoutes,
199-
location,
200-
wildCardRouteMatch,
201-
wildCardRoute.pathnameBase,
202-
allRoutes,
203-
);
195+
const [name, source] = getNormalizedName(wildCardRoutes, location, wildCardRouteMatch, basename, allRoutes);
204196

205197
if (wildCardRoute.pathnameBase && name) {
206-
return [basename + wildCardRoute.pathnameBase + name, source];
198+
return [wildCardRoute.pathnameBase + name, source];
207199
}
208200
}
209201
}
@@ -216,13 +208,12 @@ function getNormalizedName(
216208
if (route) {
217209
// Early return if index route
218210
if (route.index) {
219-
debugger;
220211
return sendIndexPath(pathBuilder, branch.pathname, basename);
221212
}
222213
const path = route.path;
223214

224215
// If path is not a wildcard and has no child routes, append the path
225-
if (path && !matchIsWildcardAndHasChildren(path, branch)) {
216+
if (path && !pathIsWildcardAndHasChildren(path, branch)) {
226217
const newPath = path[0] === '/' || pathBuilder[pathBuilder.length - 1] === '/' ? path : `/${path}`;
227218
pathBuilder += newPath;
228219

@@ -245,7 +236,7 @@ function getNormalizedName(
245236
}
246237

247238
// if the last character of the pathbuilder is a wildcard and there are children, remove the wildcard
248-
if (matchIsWildcardAndHasChildren(pathBuilder, branch)) {
239+
if (pathIsWildcardAndHasChildren(pathBuilder, branch)) {
249240
pathBuilder = pathBuilder.slice(0, -1);
250241
}
251242

@@ -256,27 +247,23 @@ function getNormalizedName(
256247
}
257248
}
258249

259-
debugger;
260250
return [_stripBasename ? stripBasenameFromPathname(location.pathname, basename) : location.pathname, 'url'];
261251
}
262252

263-
function updatePageloadTransaction(options: {
264-
activeRootSpan: Span | undefined;
265-
location: Location;
266-
routes: RouteObject[];
267-
matches?: AgnosticDataRouteMatch;
268-
basename?: string;
269-
allRoutes?: RouteObject[];
270-
}): void {
271-
debugger;
272-
const { activeRootSpan, location, routes, matches, basename, allRoutes } = options;
273-
253+
function updatePageloadTransaction(
254+
activeRootSpan: Span | undefined,
255+
location: Location,
256+
routes: RouteObject[],
257+
matches?: AgnosticDataRouteMatch,
258+
basename?: string,
259+
allRoutes?: RouteObject[],
260+
): void {
274261
const branches = Array.isArray(matches)
275262
? matches
276-
: (_matchRoutes(allRoutes, location, basename) as unknown as RouteMatch[]);
263+
: (_matchRoutes(routes, location, basename) as unknown as RouteMatch[]);
277264

278265
if (branches) {
279-
const [name, source] = getNormalizedName(allRoutes || routes, location, branches, basename, allRoutes);
266+
const [name, source] = getNormalizedName(routes, location, branches, basename, allRoutes);
280267

281268
getCurrentScope().setTransactionName(name);
282269

@@ -287,16 +274,14 @@ function updatePageloadTransaction(options: {
287274
}
288275
}
289276

290-
function handleNavigation(options: {
291-
location: Location;
292-
routes: RouteObject[];
293-
navigationType: Action;
294-
matches?: AgnosticDataRouteMatch;
295-
basename?: string;
296-
allRoutes?: RouteObject[];
297-
}): void {
298-
const { location, routes, navigationType, matches, basename, allRoutes } = options;
299-
277+
function handleNavigation(
278+
location: Location,
279+
routes: RouteObject[],
280+
navigationType: Action,
281+
matches?: AgnosticDataRouteMatch,
282+
basename?: string,
283+
allRoutes?: RouteObject[],
284+
): void {
300285
const branches = Array.isArray(matches) ? matches : _matchRoutes(routes, location, basename);
301286

302287
const client = getClient();
@@ -344,35 +329,26 @@ export function withSentryReactRouterV6Routing<P extends Record<string, any>, R
344329
}
345330

346331
const allRoutes: RouteObject[] = [];
347-
let isMountRenderPass: boolean = true;
348332

349333
const SentryRoutes: React.FC<P> = (props: P) => {
334+
const isMountRenderPass = React.useRef(true);
335+
350336
const location = _useLocation();
351337
const navigationType = _useNavigationType();
352338

353339
_useEffect(
354340
() => {
355341
const routes = _createRoutesFromChildren(props.children) as RouteObject[];
356342

357-
if (isMountRenderPass) {
358-
routes.forEach(route => {
359-
allRoutes.push(...getChildRoutesRecursively(route));
360-
});
361-
362-
updatePageloadTransaction({
363-
activeRootSpan: getActiveRootSpan(),
364-
location,
365-
routes,
366-
allRoutes,
367-
});
368-
isMountRenderPass = false;
343+
routes.forEach(route => {
344+
allRoutes.push(...getChildRoutesRecursively(route));
345+
});
346+
347+
if (isMountRenderPass.current) {
348+
updatePageloadTransaction(getActiveRootSpan(), location, routes, undefined, undefined, allRoutes);
349+
isMountRenderPass.current = false;
369350
} else {
370-
handleNavigation({
371-
location,
372-
routes,
373-
navigationType,
374-
allRoutes,
375-
});
351+
handleNavigation(location, routes, navigationType, undefined, undefined, allRoutes);
376352
}
377353
},
378354
// `props.children` is purposely not included in the dependency array, because we do not want to re-run this effect
@@ -427,26 +403,15 @@ export function wrapUseRoutes(origUseRoutes: UseRoutes): UseRoutes {
427403
const normalizedLocation =
428404
typeof stableLocationParam === 'string' ? { pathname: stableLocationParam } : stableLocationParam;
429405

430-
if (isMountRenderPass) {
431-
routes.forEach(route => {
432-
allRoutes.push(...getChildRoutesRecursively(route));
433-
});
434-
435-
updatePageloadTransaction({
436-
activeRootSpan: getActiveRootSpan(),
437-
location: normalizedLocation,
438-
routes,
439-
allRoutes,
440-
});
406+
routes.forEach(route => {
407+
allRoutes.push(...getChildRoutesRecursively(route));
408+
});
441409

410+
if (isMountRenderPass) {
411+
updatePageloadTransaction(getActiveRootSpan(), normalizedLocation, routes, undefined, undefined, allRoutes);
442412
isMountRenderPass = false;
443413
} else {
444-
handleNavigation({
445-
location: normalizedLocation,
446-
routes,
447-
navigationType,
448-
allRoutes,
449-
});
414+
handleNavigation(normalizedLocation, routes, navigationType, undefined, undefined, allRoutes);
450415
}
451416
}, [navigationType, stableLocationParam]);
452417

@@ -485,23 +450,13 @@ export function wrapCreateBrowserRouter<
485450
// This is the earliest convenient time to update the transaction name.
486451
// Callbacks to `router.subscribe` are not called for the initial load.
487452
if (router.state.historyAction === 'POP' && activeRootSpan) {
488-
updatePageloadTransaction({
489-
activeRootSpan,
490-
location: router.state.location,
491-
routes,
492-
basename,
493-
});
453+
updatePageloadTransaction(activeRootSpan, router.state.location, routes, undefined, basename);
494454
}
495455

496456
router.subscribe((state: RouterState) => {
497457
const location = state.location;
498458
if (state.historyAction === 'PUSH' || state.historyAction === 'POP') {
499-
handleNavigation({
500-
location,
501-
routes,
502-
navigationType: state.historyAction,
503-
basename,
504-
});
459+
handleNavigation(location, routes, state.historyAction, undefined, basename);
505460
}
506461
});
507462

packages/react/test/reactrouterv6.test.tsx

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -529,14 +529,8 @@ describe('reactRouterV6BrowserTracingIntegration', () => {
529529
);
530530

531531
expect(mockStartBrowserTracingPageLoadSpan).toHaveBeenCalledTimes(1);
532-
expect(mockStartBrowserTracingPageLoadSpan).toHaveBeenLastCalledWith(expect.any(BrowserClient), {
533-
name: '/projects/:projectId/views/:viewId',
534-
attributes: {
535-
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
536-
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'pageload',
537-
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.pageload.react.reactrouter_v6',
538-
},
539-
});
532+
expect(mockRootSpan.updateName).toHaveBeenLastCalledWith('/projects/:projectId/views/:viewId');
533+
expect(mockRootSpan.setAttribute).toHaveBeenLastCalledWith(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'route');
540534
});
541535

542536
it('works with descendant wildcard routes - navigation', () => {
@@ -565,6 +559,7 @@ describe('reactRouterV6BrowserTracingIntegration', () => {
565559
</Route>
566560
</Route>
567561
<Route path="*" element={<div>No Match Page</div>} />
562+
<Route path="/404" element={<div>404</div>} />
568563
</SentryRoutes>
569564
);
570565

@@ -577,9 +572,7 @@ describe('reactRouterV6BrowserTracingIntegration', () => {
577572
</MemoryRouter>,
578573
);
579574

580-
// Fixme: Check why it's called twice
581-
expect(mockStartBrowserTracingNavigationSpan).toHaveBeenCalledTimes(2);
582-
575+
expect(mockStartBrowserTracingNavigationSpan).toHaveBeenCalledTimes(1);
583576
expect(mockStartBrowserTracingNavigationSpan).toHaveBeenLastCalledWith(expect.any(BrowserClient), {
584577
name: '/projects/:projectId/views/:viewId',
585578
attributes: {

0 commit comments

Comments
 (0)