forked from getsentry/sentry-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.ts
59 lines (54 loc) · 2.27 KB
/
router.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
import { Transaction, TransactionContext } from '@sentry/types';
import { addInstrumentationHandler, logger, WINDOW } from '@sentry/utils';
/**
* Default function implementing pageload and navigation transactions
*/
export function instrumentRoutingWithDefaults<T extends Transaction>(
customStartTransaction: (context: TransactionContext) => T | undefined,
startTransactionOnPageLoad: boolean = true,
startTransactionOnLocationChange: boolean = true,
): void {
if (!WINDOW || !WINDOW.location) {
__DEBUG_BUILD__ && logger.warn('Could not initialize routing instrumentation due to invalid location');
return;
}
let startingUrl: string | undefined = WINDOW.location.href;
let activeTransaction: T | undefined;
if (startTransactionOnPageLoad) {
activeTransaction = customStartTransaction({
name: WINDOW.location.pathname,
op: 'pageload',
metadata: { source: 'url' },
});
}
if (startTransactionOnLocationChange) {
addInstrumentationHandler('history', ({ to, from }: { to: string; from?: string }) => {
/**
* This early return is there to account for some cases where a navigation transaction starts right after
* long-running pageload. We make sure that if `from` is undefined and a valid `startingURL` exists, we don't
* create an uneccessary navigation transaction.
*
* This was hard to duplicate, but this behavior stopped as soon as this fix was applied. This issue might also
* only be caused in certain development environments where the usage of a hot module reloader is causing
* errors.
*/
if (from === undefined && startingUrl && startingUrl.indexOf(to) !== -1) {
startingUrl = undefined;
return;
}
if (from !== to) {
startingUrl = undefined;
if (activeTransaction) {
__DEBUG_BUILD__ && logger.log(`[Tracing] Finishing current transaction with op: ${activeTransaction.op}`);
// If there's an open transaction on the scope, we need to finish it before creating an new one.
activeTransaction.finish();
}
activeTransaction = customStartTransaction({
name: WINDOW.location.pathname,
op: 'navigation',
metadata: { source: 'url' },
});
}
});
}
}