Skip to content

Commit 5b3e06e

Browse files
authored
fix(react): Use history object for init transaction name (#3609)
When creating pageload transactions the current react router v4/v5 relies on global.location.pathname (global is the current window instance), which can produce inconsistent basenames. This can cause pageload transctions to be not paramaterized correctly. This patch fixes this by using history.location.pathname to generate pageload transaction names. This was tested locally to confirm changes.
1 parent d1d6c41 commit 5b3e06e

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

packages/react/src/reactrouter.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ function createReactRouterInstrumentation(
5252
allRoutes: RouteConfig[] = [],
5353
matchPath?: MatchPath,
5454
): ReactRouterInstrumentation {
55+
function getInitPathName(): string | undefined {
56+
if (history && history.location) {
57+
return history.location.pathname;
58+
}
59+
60+
if (global && global.location) {
61+
return global.location.pathname;
62+
}
63+
64+
return undefined;
65+
}
66+
5567
function getTransactionName(pathname: string): string {
5668
if (allRoutes === [] || !matchPath) {
5769
return pathname;
@@ -69,9 +81,10 @@ function createReactRouterInstrumentation(
6981
}
7082

7183
return (customStartTransaction, startTransactionOnPageLoad = true, startTransactionOnLocationChange = true): void => {
72-
if (startTransactionOnPageLoad && global && global.location) {
84+
const initPathName = getInitPathName();
85+
if (startTransactionOnPageLoad && initPathName) {
7386
activeTransaction = customStartTransaction({
74-
name: getTransactionName(global.location.pathname),
87+
name: getTransactionName(initPathName),
7588
op: 'pageload',
7689
tags: {
7790
'routing.instrumentation': name,

0 commit comments

Comments
 (0)