1- import { addHandler , fill , maybeInstrument , supportsHistory , triggerHandlers } from '@sentry/core' ;
21import type { HandlerDataHistory } from '@sentry/core' ;
2+ import { addHandler , fill , maybeInstrument , supportsHistory , triggerHandlers } from '@sentry/core' ;
33import { WINDOW } from '../types' ;
44
55let lastHref : string | undefined ;
@@ -19,29 +19,26 @@ export function addHistoryInstrumentationHandler(handler: (data: HandlerDataHist
1919}
2020
2121function instrumentHistory ( ) : void {
22- if ( ! supportsHistory ( ) ) {
23- return ;
24- }
25-
26- const oldOnPopState = WINDOW . onpopstate ;
27- WINDOW . onpopstate = function ( this : WindowEventHandlers , ...args : unknown [ ] ) {
22+ // This may also be triggered on `pushState`, but it may not always reliably be emitted by the browser
23+ // Which is why we also monkey-patch methods below, in addition to this
24+ WINDOW . addEventListener ( 'popstate' , ( ) => {
2825 const to = WINDOW . location . href ;
2926 // keep track of the current URL state, as we always receive only the updated state
3027 const from = lastHref ;
3128 lastHref = to ;
32- const handlerData : HandlerDataHistory = { from, to } ;
33- triggerHandlers ( 'history' , handlerData ) ;
34- if ( oldOnPopState ) {
35- // Apparently this can throw in Firefox when incorrectly implemented plugin is installed.
36- // https://github.com/getsentry/sentry-javascript/issues/3344
37- // https://github.com/bugsnag/bugsnag-js/issues/469
38- try {
39- return oldOnPopState . apply ( this , args ) ;
40- } catch ( _oO ) {
41- // no-empty
42- }
29+
30+ if ( from === to ) {
31+ return ;
4332 }
44- } ;
33+
34+ const handlerData = { from, to } satisfies HandlerDataHistory ;
35+ triggerHandlers ( 'history' , handlerData ) ;
36+ } ) ;
37+
38+ // Just guard against this not being available, in weird environments
39+ if ( ! supportsHistory ( ) ) {
40+ return ;
41+ }
4542
4643 function historyReplacementFunction ( originalHistoryFunction : ( ) => void ) : ( ) => void {
4744 return function ( this : History , ...args : unknown [ ] ) : void {
@@ -52,7 +49,12 @@ function instrumentHistory(): void {
5249 const to = String ( url ) ;
5350 // keep track of the current URL state, as we always receive only the updated state
5451 lastHref = to ;
55- const handlerData : HandlerDataHistory = { from, to } ;
52+
53+ if ( from === to ) {
54+ return ;
55+ }
56+
57+ const handlerData = { from, to } satisfies HandlerDataHistory ;
5658 triggerHandlers ( 'history' , handlerData ) ;
5759 }
5860 return originalHistoryFunction . apply ( this , args ) ;
0 commit comments