summaryrefslogtreecommitdiff
path: root/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/router.js
diff options
context:
space:
mode:
Diffstat (limited to 'shared/logger/node_modules/@sentry-internal/tracing/esm/browser/router.js')
-rw-r--r--shared/logger/node_modules/@sentry-internal/tracing/esm/browser/router.js64
1 files changed, 64 insertions, 0 deletions
diff --git a/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/router.js b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/router.js
new file mode 100644
index 0000000..8c70bef
--- /dev/null
+++ b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/router.js
@@ -0,0 +1,64 @@
+import { logger, browserPerformanceTimeOrigin, addInstrumentationHandler } from '@sentry/utils';
+import { WINDOW } from './types.js';
+
+/**
+ * Default function implementing pageload and navigation transactions
+ */
+function instrumentRoutingWithDefaults(
+ customStartTransaction,
+ startTransactionOnPageLoad = true,
+ startTransactionOnLocationChange = true,
+) {
+ if (!WINDOW || !WINDOW.location) {
+ (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('Could not initialize routing instrumentation due to invalid location');
+ return;
+ }
+
+ let startingUrl = WINDOW.location.href;
+
+ let activeTransaction;
+ if (startTransactionOnPageLoad) {
+ activeTransaction = customStartTransaction({
+ name: WINDOW.location.pathname,
+ // pageload should always start at timeOrigin (and needs to be in s, not ms)
+ startTimestamp: browserPerformanceTimeOrigin ? browserPerformanceTimeOrigin / 1000 : undefined,
+ op: 'pageload',
+ metadata: { source: 'url' },
+ });
+ }
+
+ if (startTransactionOnLocationChange) {
+ addInstrumentationHandler('history', ({ to, from }) => {
+ /**
+ * 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) {
+ (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && 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' },
+ });
+ }
+ });
+ }
+}
+
+export { instrumentRoutingWithDefaults };
+//# sourceMappingURL=router.js.map