forked from getsentry/sentry-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.ts
66 lines (56 loc) · 2.33 KB
/
history.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import type { HandlerDataHistory } from '@sentry/core';
import { addHandler, fill, maybeInstrument, supportsHistory, triggerHandlers } from '@sentry/core';
import { WINDOW } from '../types';
let lastHref: string | undefined;
/**
* Add an instrumentation handler for when a fetch request happens.
* The handler function is called once when the request starts and once when it ends,
* which can be identified by checking if it has an `endTimestamp`.
*
* Use at your own risk, this might break without changelog notice, only used internally.
* @hidden
*/
export function addHistoryInstrumentationHandler(handler: (data: HandlerDataHistory) => void): void {
const type = 'history';
addHandler(type, handler);
maybeInstrument(type, instrumentHistory);
}
function instrumentHistory(): void {
// 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;
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 {
const url = args.length > 2 ? args[2] : undefined;
if (url) {
// coerce to string (this is what pushState does)
const from = lastHref;
const to = String(url);
// keep track of the current URL state, as we always receive only the updated state
lastHref = to;
if (from === to) {
return originalHistoryFunction.apply(this, args);
}
const handlerData = { from, to } satisfies HandlerDataHistory;
triggerHandlers('history', handlerData);
}
return originalHistoryFunction.apply(this, args);
};
}
fill(WINDOW.history, 'pushState', historyReplacementFunction);
fill(WINDOW.history, 'replaceState', historyReplacementFunction);
}