Skip to content

ref(browser): Update supportsHistory check & history usage #14696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions packages/browser-utils/src/instrument/history.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { addHandler, fill, maybeInstrument, supportsHistory, triggerHandlers } from '@sentry/core';
import type { HandlerDataHistory } from '@sentry/core';
import { addHandler, fill, maybeInstrument, supportsHistory, triggerHandlers } from '@sentry/core';
import { WINDOW } from '../types';

let lastHref: string | undefined;
Expand All @@ -19,29 +19,26 @@ export function addHistoryInstrumentationHandler(handler: (data: HandlerDataHist
}

function instrumentHistory(): void {
if (!supportsHistory()) {
return;
}

const oldOnPopState = WINDOW.onpopstate;
WINDOW.onpopstate = function (this: WindowEventHandlers, ...args: unknown[]) {
// The `popstate` event may also be triggered on `pushState`, but it may not always reliably be emitted by the browser
// Which is why we also monkey-patch methods below, in addition to this
WINDOW.addEventListener('popstate', () => {
const to = WINDOW.location.href;
// keep track of the current URL state, as we always receive only the updated state
const from = lastHref;
lastHref = to;
const handlerData: HandlerDataHistory = { from, to };
triggerHandlers('history', handlerData);
if (oldOnPopState) {
// Apparently this can throw in Firefox when incorrectly implemented plugin is installed.
// https://github.com/getsentry/sentry-javascript/issues/3344
// https://github.com/bugsnag/bugsnag-js/issues/469
try {
return oldOnPopState.apply(this, args);
} catch (_oO) {
// no-empty
}

if (from === to) {
return;
}
};

const handlerData = { from, to } satisfies HandlerDataHistory;
triggerHandlers('history', handlerData);
});

// Just guard against this not being available, in weird environments
if (!supportsHistory()) {
return;
}

function historyReplacementFunction(originalHistoryFunction: () => void): () => void {
return function (this: History, ...args: unknown[]): void {
Expand All @@ -52,7 +49,12 @@ function instrumentHistory(): void {
const to = String(url);
// keep track of the current URL state, as we always receive only the updated state
lastHref = to;
const handlerData: HandlerDataHistory = { from, to };

if (from === to) {
return;
}

const handlerData = { from, to } satisfies HandlerDataHistory;
triggerHandlers('history', handlerData);
}
return originalHistoryFunction.apply(this, args);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utils-hoist/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export {
supportsDOMException,
supportsErrorEvent,
supportsFetch,
supportsHistory,
supportsNativeFetch,
supportsReferrerPolicy,
supportsReportingObserver,
Expand Down Expand Up @@ -178,4 +179,3 @@ export { vercelWaitUntil } from './vercelWaitUntil';
export { SDK_VERSION } from './version';
export { getDebugImagesForResources, getFilenameToDebugIdMap } from './debug-ids';
export { escapeStringForRegex } from './vendor/escapeStringForRegex';
export { supportsHistory } from './vendor/supportsHistory';
12 changes: 10 additions & 2 deletions packages/core/src/utils-hoist/supports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ const WINDOW = GLOBAL_OBJ as unknown as Window;

declare const EdgeRuntime: string | undefined;

export { supportsHistory } from './vendor/supportsHistory';

/**
* Tells whether current environment supports ErrorEvent objects
* {@link supportsErrorEvent}.
Expand Down Expand Up @@ -56,6 +54,16 @@ export function supportsDOMException(): boolean {
}
}

/**
* Tells whether current environment supports History API
* {@link supportsHistory}.
*
* @returns Answer to the given question.
*/
export function supportsHistory(): boolean {
return 'history' in WINDOW;
}

/**
* Tells whether current environment supports Fetch API
* {@link supportsFetch}.
Expand Down
44 changes: 0 additions & 44 deletions packages/core/src/utils-hoist/vendor/supportsHistory.ts

This file was deleted.

Loading