Skip to content

Commit

Permalink
fix(react-router-v3): makes normalizeTransactionName take a callback …
Browse files Browse the repository at this point in the history
…function (#2946)
  • Loading branch information
Stephen Cefali authored Sep 30, 2020
1 parent 0ee0799 commit 1a17399
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions packages/react/src/reactrouterv3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ export function reactRouterV3Instrumentation(

// Have to use global.location because history.location might not be defined.
if (startTransactionOnPageLoad && global && global.location) {
prevName = normalizeTransactionName(routes, (global.location as unknown) as Location, match);

activeTransaction = startTransaction({
name: prevName,
op: 'pageload',
tags: {
'routing.instrumentation': 'react-router-v3',
},
normalizeTransactionName(routes, (global.location as unknown) as Location, match, (localName: string) => {
prevName = localName;
activeTransaction = startTransaction({
name: prevName,
op: 'pageload',
tags: {
'routing.instrumentation': 'react-router-v3',
},
});
});
}

Expand All @@ -65,11 +66,13 @@ export function reactRouterV3Instrumentation(
if (prevName) {
tags.from = prevName;
}
prevName = normalizeTransactionName(routes, location, match);
activeTransaction = startTransaction({
name: prevName,
op: 'navigation',
tags,
normalizeTransactionName(routes, location, match, (localName: string) => {
prevName = localName;
activeTransaction = startTransaction({
name: prevName,
op: 'navigation',
tags,
});
});
}
});
Expand All @@ -80,7 +83,12 @@ export function reactRouterV3Instrumentation(
/**
* Normalize transaction names using `Router.match`
*/
function normalizeTransactionName(appRoutes: Route[], location: Location, match: Match): string {
function normalizeTransactionName(
appRoutes: Route[],
location: Location,
match: Match,
callback: (pathname: string) => void,
): void {
let name = location.pathname;
match(
{
Expand All @@ -89,19 +97,18 @@ function normalizeTransactionName(appRoutes: Route[], location: Location, match:
},
(error, _redirectLocation, renderProps) => {
if (error || !renderProps) {
return name;
return callback(name);
}

const routePath = getRouteStringFromRoutes(renderProps.routes || []);
if (routePath.length === 0 || routePath === '/*') {
return name;
return callback(name);
}

name = routePath;
return name;
return callback(name);
},
);
return name;
}

/**
Expand Down

0 comments on commit 1a17399

Please sign in to comment.