this repo has no description
1import { logger, browserPerformanceTimeOrigin, addInstrumentationHandler } from '@sentry/utils';
2import { WINDOW } from './types.js';
3
4/**
5 * Default function implementing pageload and navigation transactions
6 */
7function instrumentRoutingWithDefaults(
8 customStartTransaction,
9 startTransactionOnPageLoad = true,
10 startTransactionOnLocationChange = true,
11) {
12 if (!WINDOW || !WINDOW.location) {
13 (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('Could not initialize routing instrumentation due to invalid location');
14 return;
15 }
16
17 let startingUrl = WINDOW.location.href;
18
19 let activeTransaction;
20 if (startTransactionOnPageLoad) {
21 activeTransaction = customStartTransaction({
22 name: WINDOW.location.pathname,
23 // pageload should always start at timeOrigin (and needs to be in s, not ms)
24 startTimestamp: browserPerformanceTimeOrigin ? browserPerformanceTimeOrigin / 1000 : undefined,
25 op: 'pageload',
26 metadata: { source: 'url' },
27 });
28 }
29
30 if (startTransactionOnLocationChange) {
31 addInstrumentationHandler('history', ({ to, from }) => {
32 /**
33 * This early return is there to account for some cases where a navigation transaction starts right after
34 * long-running pageload. We make sure that if `from` is undefined and a valid `startingURL` exists, we don't
35 * create an uneccessary navigation transaction.
36 *
37 * This was hard to duplicate, but this behavior stopped as soon as this fix was applied. This issue might also
38 * only be caused in certain development environments where the usage of a hot module reloader is causing
39 * errors.
40 */
41 if (from === undefined && startingUrl && startingUrl.indexOf(to) !== -1) {
42 startingUrl = undefined;
43 return;
44 }
45
46 if (from !== to) {
47 startingUrl = undefined;
48 if (activeTransaction) {
49 (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log(`[Tracing] Finishing current transaction with op: ${activeTransaction.op}`);
50 // If there's an open transaction on the scope, we need to finish it before creating an new one.
51 activeTransaction.finish();
52 }
53 activeTransaction = customStartTransaction({
54 name: WINDOW.location.pathname,
55 op: 'navigation',
56 metadata: { source: 'url' },
57 });
58 }
59 });
60 }
61}
62
63export { instrumentRoutingWithDefaults };
64//# sourceMappingURL=router.js.map