@@ -113,9 +113,20 @@ const DEFAULT_TAGS = {
113
113
} as const ;
114
114
115
115
let activeTransaction : Transaction | undefined = undefined ;
116
- let prevTransactionName : string | undefined = undefined ;
117
116
let startTransaction : StartTransactionCb | undefined = undefined ;
118
117
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
+
119
130
const client = getCurrentHub ( ) . getClient ( ) ;
120
131
121
132
/**
@@ -137,6 +148,8 @@ export function nextRouterInstrumentation(
137
148
const { route, traceParentData, baggage, params } = extractNextDataTagInformation ( ) ;
138
149
139
150
prevTransactionName = route || global . location . pathname ;
151
+ previousLocation = global . location . pathname ;
152
+
140
153
const source = route ? 'route' : 'url' ;
141
154
142
155
activeTransaction = startTransactionCb ( {
@@ -197,19 +210,25 @@ function changeStateWrapper(originalChangeStateWrapper: RouterChangeState): Wrap
197
210
...args : any [ ]
198
211
) : Promise < boolean > {
199
212
const newTransactionName = stripUrlQueryAndFragment ( url ) ;
213
+
200
214
// 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
+
202
218
if ( activeTransaction ) {
203
219
activeTransaction . finish ( ) ;
204
220
}
221
+
205
222
const tags : Record < string , Primitive > = {
206
223
...DEFAULT_TAGS ,
207
224
method,
208
225
...options ,
209
226
} ;
227
+
210
228
if ( prevTransactionName ) {
211
229
tags . from = prevTransactionName ;
212
230
}
231
+
213
232
prevTransactionName = newTransactionName ;
214
233
activeTransaction = startTransaction ( {
215
234
name : prevTransactionName ,
0 commit comments