Skip to content

Commit 7633952

Browse files
author
Luca Forstner
authored
fix(nextjs): Start navigation transactions on same-route navigations (#5642)
1 parent e8c78c4 commit 7633952

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

packages/nextjs/src/performance/client.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,20 @@ const DEFAULT_TAGS = {
113113
} as const;
114114

115115
let activeTransaction: Transaction | undefined = undefined;
116-
let prevTransactionName: string | undefined = undefined;
117116
let startTransaction: StartTransactionCb | undefined = undefined;
118117

118+
// We keep track of the previous page location so we can avoid creating transactions when navigating to the same page.
119+
// This variable should always contain a pathname. (without query string or fragment)
120+
// We are making a tradeoff by not starting transactions when just the query string changes. One could argue that we
121+
// should in fact start transactions when the query changes, however, in some cases (for example when typing in a search
122+
// box) the query might change multiple times a second, resulting in way too many transactions.
123+
// Because we currently don't have a real way of preventing transactions to be created in this case (except for the
124+
// shotgun approach `startTransactionOnLocationChange: false`), we won't start transactions when *just* the query changes.
125+
let previousLocation: string | undefined = undefined;
126+
127+
// We keep track of the previous transaction name so we can set the `from` field on navigation transactions.
128+
let prevTransactionName: string | undefined = undefined;
129+
119130
const client = getCurrentHub().getClient();
120131

121132
/**
@@ -137,6 +148,8 @@ export function nextRouterInstrumentation(
137148
const { route, traceParentData, baggage, params } = extractNextDataTagInformation();
138149

139150
prevTransactionName = route || global.location.pathname;
151+
previousLocation = global.location.pathname;
152+
140153
const source = route ? 'route' : 'url';
141154

142155
activeTransaction = startTransactionCb({
@@ -197,19 +210,25 @@ function changeStateWrapper(originalChangeStateWrapper: RouterChangeState): Wrap
197210
...args: any[]
198211
): Promise<boolean> {
199212
const newTransactionName = stripUrlQueryAndFragment(url);
213+
200214
// do not start a transaction if it's from the same page
201-
if (startTransaction !== undefined && prevTransactionName !== newTransactionName) {
215+
if (startTransaction !== undefined && previousLocation !== as) {
216+
previousLocation = as;
217+
202218
if (activeTransaction) {
203219
activeTransaction.finish();
204220
}
221+
205222
const tags: Record<string, Primitive> = {
206223
...DEFAULT_TAGS,
207224
method,
208225
...options,
209226
};
227+
210228
if (prevTransactionName) {
211229
tags.from = prevTransactionName;
212230
}
231+
213232
prevTransactionName = newTransactionName;
214233
activeTransaction = startTransaction({
215234
name: prevTransactionName,

0 commit comments

Comments
 (0)