router.js 2.6 KB

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