Skip to content

Commit cee11ea

Browse files
committed
fix by using to from history instrumentation
1 parent 694883d commit cee11ea

File tree

3 files changed

+54
-23
lines changed

3 files changed

+54
-23
lines changed

packages/browser-utils/src/instrument/history.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,15 @@ export function instrumentHistory(): void {
4747
return function (this: History, ...args: unknown[]): void {
4848
const url = args.length > 2 ? args[2] : undefined;
4949
if (url) {
50-
// coerce to string (this is what pushState does)
5150
const from = lastHref;
52-
const to = String(url);
51+
52+
// Ensure the URL is absolute
53+
// this can be either a path, then it is relative to the current origin
54+
// or a full URL of the current origin - other origins are not allowed
55+
// See: https://developer.mozilla.org/en-US/docs/Web/API/History/pushState#url
56+
// coerce to string (this is what pushState does)
57+
const to = getAbsoluteUrl(String(url));
58+
5359
// keep track of the current URL state, as we always receive only the updated state
5460
lastHref = to;
5561

@@ -67,3 +73,13 @@ export function instrumentHistory(): void {
6773
fill(WINDOW.history, 'pushState', historyReplacementFunction);
6874
fill(WINDOW.history, 'replaceState', historyReplacementFunction);
6975
}
76+
77+
function getAbsoluteUrl(urlOrPath: string): string {
78+
try {
79+
const url = new URL(urlOrPath, WINDOW.location.origin);
80+
return url.toString();
81+
} catch {
82+
// fallback, just do nothing
83+
return urlOrPath;
84+
}
85+
}

packages/browser/src/tracing/browserTracingIntegration.ts

+34-21
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
getLocationHref,
1313
GLOBAL_OBJ,
1414
logger,
15+
parseStringToURLObject,
1516
propagationContextFromHeaders,
1617
registerSpanErrorInstrumentation,
1718
SEMANTIC_ATTRIBUTE_SENTRY_IDLE_SPAN_FINISH_REASON,
@@ -355,21 +356,11 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
355356
sampled: spanIsSampled(idleSpan),
356357
dsc: getDynamicSamplingContextFromSpan(span),
357358
});
358-
359-
scope.setSDKProcessingMetadata({
360-
normalizedRequest: undefined,
361-
});
362359
},
363360
});
364361

365362
setActiveIdleSpan(client, idleSpan);
366363

367-
// We store the normalized request data on the scope, so we get the request data at time of span creation
368-
// otherwise, the URL etc. may already be of the following navigation, and we'd report the wrong URL
369-
getCurrentScope().setSDKProcessingMetadata({
370-
normalizedRequest: getHttpRequestData(),
371-
});
372-
373364
function emitFinish(): void {
374365
if (optionalWindowDocument && ['interactive', 'complete'].includes(optionalWindowDocument.readyState)) {
375366
client.emit('idleSpanEnableAutoFinish', idleSpan);
@@ -409,7 +400,14 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
409400
maybeEndActiveSpan();
410401

411402
getIsolationScope().setPropagationContext({ traceId: generateTraceId(), sampleRand: Math.random() });
412-
getCurrentScope().setPropagationContext({ traceId: generateTraceId(), sampleRand: Math.random() });
403+
404+
const scope = getCurrentScope();
405+
scope.setPropagationContext({ traceId: generateTraceId(), sampleRand: Math.random() });
406+
// We reset this to ensure we do not have lingering incorrect data here
407+
// places that call this hook may set this where appropriate - else, the URL at span sending time is used
408+
scope.setSDKProcessingMetadata({
409+
normalizedRequest: undefined,
410+
});
413411

414412
_createRouteSpan(client, {
415413
op: 'navigation',
@@ -427,7 +425,15 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
427425
const baggage = traceOptions.baggage || getMetaContent('baggage');
428426

429427
const propagationContext = propagationContextFromHeaders(sentryTrace, baggage);
430-
getCurrentScope().setPropagationContext(propagationContext);
428+
429+
const scope = getCurrentScope();
430+
scope.setPropagationContext(propagationContext);
431+
432+
// We store the normalized request data on the scope, so we get the request data at time of span creation
433+
// otherwise, the URL etc. may already be of the following navigation, and we'd report the wrong URL
434+
scope.setSDKProcessingMetadata({
435+
normalizedRequest: getHttpRequestData(),
436+
});
431437

432438
_createRouteSpan(client, {
433439
op: 'pageload',
@@ -470,16 +476,23 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
470476
}
471477

472478
startingUrl = undefined;
479+
const parsed = parseStringToURLObject(to);
480+
startBrowserTracingNavigationSpan(client, {
481+
name: parsed?.pathname || WINDOW.location.pathname,
482+
attributes: {
483+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
484+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.navigation.browser',
485+
},
486+
});
473487

474-
// We wait a tick here to ensure that WINDOW.location.pathname is updated
475-
setTimeout(() => {
476-
startBrowserTracingNavigationSpan(client, {
477-
name: WINDOW.location.pathname,
478-
attributes: {
479-
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
480-
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.navigation.browser',
481-
},
482-
});
488+
// We store the normalized request data on the scope, so we get the request data at time of span creation
489+
// otherwise, the URL etc. may already be of the following navigation, and we'd report the wrong URL
490+
getCurrentScope().setSDKProcessingMetadata({
491+
normalizedRequest: {
492+
...getHttpRequestData(),
493+
// Ensure to set this, so this matches the target route even if the URL has not yet been updated
494+
url: to,
495+
},
483496
});
484497
});
485498
}

packages/core/src/types-hoist/instrument.ts

+2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ export interface HandlerDataConsole {
7878
}
7979

8080
export interface HandlerDataHistory {
81+
/** The full URL of the previous page */
8182
from: string | undefined;
83+
/** The full URL of the new page */
8284
to: string;
8385
}
8486

0 commit comments

Comments
 (0)