@mushi-mushi/web 1.22.1 → 1.22.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -2553,7 +2553,7 @@ function getWidgetStyles(theme, accent = "", accentText = "") {
2553
2553
 
2554
2554
  // src/version.ts
2555
2555
  var MUSHI_SDK_PACKAGE = "@mushi-mushi/web";
2556
- var MUSHI_SDK_VERSION = "1.22.1" ;
2556
+ var MUSHI_SDK_VERSION = "1.22.3" ;
2557
2557
  var CATEGORY_ICONS = {
2558
2558
  bug: "\u26A0\uFE0F",
2559
2559
  slow: "\u{1F40C}",
@@ -5403,6 +5403,101 @@ function exposeMarketingRecorder(widget) {
5403
5403
  };
5404
5404
  globalThis.__mushiRecorder = api;
5405
5405
  }
5406
+
5407
+ // src/history-patch.ts
5408
+ var capturedPushState = null;
5409
+ var capturedReplaceState = null;
5410
+ var nativePushState = null;
5411
+ var nativeReplaceState = null;
5412
+ var pushWrapper = null;
5413
+ var replaceWrapper = null;
5414
+ var popListener = null;
5415
+ var patched = false;
5416
+ var subscribers = /* @__PURE__ */ new Set();
5417
+ function notifyPush() {
5418
+ for (const sub of subscribers) {
5419
+ try {
5420
+ sub.onPush?.();
5421
+ } catch {
5422
+ }
5423
+ }
5424
+ }
5425
+ function notifyReplace() {
5426
+ for (const sub of subscribers) {
5427
+ try {
5428
+ sub.onReplace?.();
5429
+ } catch {
5430
+ }
5431
+ }
5432
+ }
5433
+ function notifyPop() {
5434
+ for (const sub of subscribers) {
5435
+ try {
5436
+ sub.onPop?.();
5437
+ } catch {
5438
+ }
5439
+ }
5440
+ }
5441
+ function ensurePatch() {
5442
+ if (patched || typeof window === "undefined") return;
5443
+ nativePushState = History.prototype.pushState;
5444
+ nativeReplaceState = History.prototype.replaceState;
5445
+ capturedPushState = nativePushState.bind(history);
5446
+ capturedReplaceState = nativeReplaceState.bind(history);
5447
+ pushWrapper = function historyPatchPushState(...args) {
5448
+ const call = capturedPushState ?? History.prototype.pushState.bind(history);
5449
+ const ret = call(...args);
5450
+ notifyPush();
5451
+ return ret;
5452
+ };
5453
+ replaceWrapper = function historyPatchReplaceState(...args) {
5454
+ const call = capturedReplaceState ?? History.prototype.replaceState.bind(history);
5455
+ const ret = call(...args);
5456
+ notifyReplace();
5457
+ return ret;
5458
+ };
5459
+ history.pushState = pushWrapper;
5460
+ history.replaceState = replaceWrapper;
5461
+ popListener = () => notifyPop();
5462
+ window.addEventListener("popstate", popListener);
5463
+ patched = true;
5464
+ }
5465
+ function uninstallPatch() {
5466
+ if (!patched || typeof window === "undefined") return;
5467
+ if (popListener) {
5468
+ window.removeEventListener("popstate", popListener);
5469
+ popListener = null;
5470
+ }
5471
+ if (pushWrapper && history.pushState === pushWrapper && nativePushState) {
5472
+ history.pushState = nativePushState;
5473
+ }
5474
+ if (replaceWrapper && history.replaceState === replaceWrapper && nativeReplaceState) {
5475
+ history.replaceState = nativeReplaceState;
5476
+ }
5477
+ pushWrapper = null;
5478
+ replaceWrapper = null;
5479
+ capturedPushState = null;
5480
+ capturedReplaceState = null;
5481
+ nativePushState = null;
5482
+ nativeReplaceState = null;
5483
+ patched = false;
5484
+ }
5485
+ function subscribeHistory(sub) {
5486
+ ensurePatch();
5487
+ subscribers.add(sub);
5488
+ return () => {
5489
+ subscribers.delete(sub);
5490
+ if (subscribers.size === 0) {
5491
+ uninstallPatch();
5492
+ }
5493
+ };
5494
+ }
5495
+ function uninstallHistoryPatchForce() {
5496
+ subscribers.clear();
5497
+ uninstallPatch();
5498
+ }
5499
+
5500
+ // src/rewards.ts
5406
5501
  var MIN_FLUSH_INTERVAL = 3e4;
5407
5502
  var DEFAULT_FLUSH_INTERVAL = 3e5;
5408
5503
  var DWELL_SAMPLE_INTERVAL = 6e4;
@@ -5579,13 +5674,40 @@ async function fetchLeaderboard(limit = 10) {
5579
5674
  return null;
5580
5675
  }
5581
5676
  }
5677
+ function teardown() {
5678
+ if (flushTimer) {
5679
+ clearInterval(flushTimer);
5680
+ flushTimer = null;
5681
+ }
5682
+ if (dwellTimer) {
5683
+ clearInterval(dwellTimer);
5684
+ dwellTimer = null;
5685
+ }
5686
+ removeActivityListeners();
5687
+ removeTierBadge();
5688
+ if (consentHost) {
5689
+ consentHost.remove();
5690
+ consentHost = null;
5691
+ }
5692
+ pendingEvents = [];
5693
+ currentUserId = null;
5694
+ apiClient = null;
5695
+ optedIn = false;
5696
+ tierCache = null;
5697
+ rewardsApiBackoffUntil = 0;
5698
+ }
5582
5699
  var routeObserver = null;
5583
5700
  var clickHandler = null;
5584
- var origPushState = null;
5701
+ var historyUnsub = null;
5585
5702
  var lastRoute = "";
5586
5703
  var listenersInstalled = false;
5587
5704
  function installActivityListeners(projectId) {
5588
- if (listenersInstalled) return;
5705
+ if (listenersInstalled && historyUnsub) {
5706
+ return;
5707
+ }
5708
+ if (listenersInstalled) {
5709
+ removeActivityListeners();
5710
+ }
5589
5711
  listenersInstalled = true;
5590
5712
  const emitRoute = () => {
5591
5713
  const route = location.pathname;
@@ -5597,12 +5719,11 @@ function installActivityListeners(projectId) {
5597
5719
  enqueue({ action: "screen_view_unique_per_day", metadata: { route } });
5598
5720
  }
5599
5721
  };
5600
- origPushState = history.pushState.bind(history);
5601
- history.pushState = function(...args) {
5602
- origPushState(...args);
5603
- emitRoute();
5604
- };
5605
- window.addEventListener("popstate", emitRoute);
5722
+ historyUnsub = subscribeHistory({
5723
+ onPush: emitRoute,
5724
+ onReplace: emitRoute,
5725
+ onPop: emitRoute
5726
+ });
5606
5727
  emitRoute();
5607
5728
  routeObserver = new MutationObserver(() => emitRoute());
5608
5729
  const main = document.querySelector("main") ?? document.body;
@@ -5616,6 +5737,17 @@ function installActivityListeners(projectId) {
5616
5737
  };
5617
5738
  document.addEventListener("click", clickHandler, { capture: true, passive: true });
5618
5739
  }
5740
+ function removeActivityListeners() {
5741
+ listenersInstalled = false;
5742
+ historyUnsub?.();
5743
+ historyUnsub = null;
5744
+ routeObserver?.disconnect();
5745
+ routeObserver = null;
5746
+ if (clickHandler) {
5747
+ document.removeEventListener("click", clickHandler, { capture: true });
5748
+ clickHandler = null;
5749
+ }
5750
+ }
5619
5751
  var badgeHost = null;
5620
5752
  function renderTierBadge(tier, config) {
5621
5753
  if (!config.showInWidget) return;
@@ -5664,6 +5796,10 @@ function renderTierBadge(tier, config) {
5664
5796
  `;
5665
5797
  document.body.appendChild(badgeHost);
5666
5798
  }
5799
+ function removeTierBadge() {
5800
+ badgeHost?.remove();
5801
+ badgeHost = null;
5802
+ }
5667
5803
  var consentHost = null;
5668
5804
  function renderConsentBanner(projectId, config) {
5669
5805
  if (consentHost) return;
@@ -5738,11 +5874,12 @@ var MAX_MESSAGE_LENGTH = 500;
5738
5874
  function createConsoleCapture() {
5739
5875
  const entries = [];
5740
5876
  const originals = {};
5877
+ const wrappers = {};
5741
5878
  const levels = ["log", "warn", "error", "info", "debug"];
5742
5879
  for (const level of levels) {
5743
5880
  const original = console[level];
5744
5881
  originals[level] = original;
5745
- console[level] = (...args) => {
5882
+ const wrapper = (...args) => {
5746
5883
  const message = args.map((arg) => {
5747
5884
  try {
5748
5885
  return typeof arg === "string" ? arg : JSON.stringify(arg);
@@ -5764,6 +5901,8 @@ function createConsoleCapture() {
5764
5901
  }
5765
5902
  original.call(console, ...args);
5766
5903
  };
5904
+ wrappers[level] = wrapper;
5905
+ console[level] = wrapper;
5767
5906
  }
5768
5907
  return {
5769
5908
  getEntries() {
@@ -5774,7 +5913,7 @@ function createConsoleCapture() {
5774
5913
  },
5775
5914
  destroy() {
5776
5915
  for (const level of levels) {
5777
- if (originals[level]) {
5916
+ if (originals[level] && console[level] === wrappers[level]) {
5778
5917
  console[level] = originals[level];
5779
5918
  }
5780
5919
  }
@@ -5872,7 +6011,8 @@ function createNetworkCapture(options = {}) {
5872
6011
  const entries = [];
5873
6012
  const originalFetch = globalThis.fetch;
5874
6013
  let activeOptions = options;
5875
- globalThis.fetch = async function mushiFetchInterceptor(input, init) {
6014
+ let fetchWrapper;
6015
+ globalThis.fetch = fetchWrapper = async function mushiFetchInterceptor(input, init) {
5876
6016
  const startTime = Date.now();
5877
6017
  const method = init?.method?.toUpperCase() ?? "GET";
5878
6018
  const url = getRequestUrl(input);
@@ -5936,7 +6076,9 @@ function createNetworkCapture(options = {}) {
5936
6076
  activeOptions = nextOptions;
5937
6077
  },
5938
6078
  destroy() {
5939
- globalThis.fetch = originalFetch;
6079
+ if (globalThis.fetch === fetchWrapper) {
6080
+ globalThis.fetch = originalFetch;
6081
+ }
5940
6082
  }
5941
6083
  };
5942
6084
  }
@@ -6388,9 +6530,6 @@ function createElementSelector() {
6388
6530
  var MAX_TIMELINE_ENTRIES = 120;
6389
6531
  function createTimelineCapture() {
6390
6532
  const entries = [];
6391
- const originalPushState = history.pushState;
6392
- const originalReplaceState = history.replaceState;
6393
- const handlePopState = () => recordRoute("popstate");
6394
6533
  const handleHashChange = () => recordRoute("hashchange");
6395
6534
  recordRoute("initial");
6396
6535
  function record(entry) {
@@ -6423,17 +6562,11 @@ function createTimelineCapture() {
6423
6562
  }
6424
6563
  });
6425
6564
  }
6426
- history.pushState = function mushiPushState(...args) {
6427
- const result = originalPushState.apply(this, args);
6428
- recordRoute("pushState");
6429
- return result;
6430
- };
6431
- history.replaceState = function mushiReplaceState(...args) {
6432
- const result = originalReplaceState.apply(this, args);
6433
- recordRoute("replaceState");
6434
- return result;
6435
- };
6436
- window.addEventListener("popstate", handlePopState);
6565
+ const unsubHistory = subscribeHistory({
6566
+ onPush: () => recordRoute("pushState"),
6567
+ onReplace: () => recordRoute("replaceState"),
6568
+ onPop: () => recordRoute("popstate")
6569
+ });
6437
6570
  window.addEventListener("hashchange", handleHashChange);
6438
6571
  document.addEventListener("click", handleClick, true);
6439
6572
  return {
@@ -6473,9 +6606,7 @@ function createTimelineCapture() {
6473
6606
  entries.length = 0;
6474
6607
  },
6475
6608
  destroy() {
6476
- history.pushState = originalPushState;
6477
- history.replaceState = originalReplaceState;
6478
- window.removeEventListener("popstate", handlePopState);
6609
+ unsubHistory();
6479
6610
  window.removeEventListener("hashchange", handleHashChange);
6480
6611
  document.removeEventListener("click", handleClick, true);
6481
6612
  }
@@ -6635,32 +6766,16 @@ function createDiscoveryCapture(opts) {
6635
6766
  flushNow: () => void 0
6636
6767
  };
6637
6768
  }
6638
- const originalPush = window.history.pushState.bind(window.history);
6639
- const originalReplace = window.history.replaceState.bind(window.history);
6640
- const patchedPush = function patched(...args) {
6641
- const out = originalPush(...args);
6642
- onMaybeNavigation();
6643
- return out;
6644
- };
6645
- const patchedReplace = function patched(...args) {
6646
- const out = originalReplace(...args);
6647
- onMaybeNavigation();
6648
- return out;
6649
- };
6650
- window.history.pushState = patchedPush;
6651
- window.history.replaceState = patchedReplace;
6652
- const onPop = () => onMaybeNavigation();
6653
- window.addEventListener("popstate", onPop);
6769
+ const onNav = () => onMaybeNavigation();
6770
+ const unsubHistory = subscribeHistory({
6771
+ onPush: onNav,
6772
+ onReplace: onNav,
6773
+ onPop: onNav
6774
+ });
6654
6775
  scheduleEmit();
6655
6776
  return {
6656
6777
  destroy() {
6657
- window.removeEventListener("popstate", onPop);
6658
- if (window.history.pushState === patchedPush) {
6659
- window.history.pushState = originalPush;
6660
- }
6661
- if (window.history.replaceState === patchedReplace) {
6662
- window.history.replaceState = originalReplace;
6663
- }
6778
+ unsubHistory();
6664
6779
  if (pendingTimer) {
6665
6780
  clearTimeout(pendingTimer);
6666
6781
  pendingTimer = null;
@@ -7244,28 +7359,14 @@ function setupProactiveTriggers(callbacks, config = {}) {
7244
7359
  let timer = null;
7245
7360
  let lastPath = window.location?.pathname ?? "";
7246
7361
  arm2();
7247
- const history2 = window.history;
7248
- const origPush = history2?.pushState;
7249
- const origReplace = history2?.replaceState;
7250
- if (history2 && origPush && origReplace) {
7251
- history2.pushState = function(...args) {
7252
- const result = origPush.apply(this, args);
7253
- reset2();
7254
- return result;
7255
- };
7256
- history2.replaceState = function(...args) {
7257
- const result = origReplace.apply(this, args);
7258
- reset2();
7259
- return result;
7260
- };
7261
- }
7262
- const onPop = () => reset2();
7263
- window.addEventListener("popstate", onPop);
7362
+ const unsubHistory = subscribeHistory({
7363
+ onPush: reset2,
7364
+ onReplace: reset2,
7365
+ onPop: reset2
7366
+ });
7264
7367
  cleanups.push(() => {
7265
7368
  if (timer) clearTimeout(timer);
7266
- window.removeEventListener("popstate", onPop);
7267
- if (history2 && origPush) history2.pushState = origPush;
7268
- if (history2 && origReplace) history2.replaceState = origReplace;
7369
+ unsubHistory();
7269
7370
  });
7270
7371
  }
7271
7372
  const firstSessionEnabled = config.firstSession === true || typeof config.firstSession === "object" && config.firstSession !== null;
@@ -8390,15 +8491,17 @@ function createInstance(config) {
8390
8491
  networkCap?.destroy();
8391
8492
  perfCap?.destroy();
8392
8493
  elementSelector?.deactivate();
8393
- timelineCap.destroy();
8494
+ teardown();
8495
+ detachAutoBreadcrumbs?.();
8496
+ detachAutoBreadcrumbs = null;
8394
8497
  discoveryCap?.destroy();
8395
8498
  discoveryCap = null;
8499
+ timelineCap.destroy();
8500
+ uninstallHistoryPatchForce();
8396
8501
  replayCap?.destroy();
8397
8502
  replayCap = null;
8398
8503
  offlineQueue.stopAutoSync();
8399
8504
  stopReporterInboxPolling();
8400
- detachAutoBreadcrumbs?.();
8401
- detachAutoBreadcrumbs = null;
8402
8505
  breadcrumbs.clear();
8403
8506
  listeners.clear();
8404
8507
  instance = null;
@@ -9060,37 +9163,18 @@ function installAutoBreadcrumbs(buffer) {
9060
9163
  data: { url: window.location.href, kind }
9061
9164
  });
9062
9165
  };
9063
- const onPop = () => dispatchRouteChange("popstate");
9064
- window.addEventListener("popstate", onPop, { passive: true });
9065
- cleanups.push(() => window.removeEventListener("popstate", onPop));
9066
- const origPush = window.history.pushState;
9067
- const origReplace = window.history.replaceState;
9068
- window.history.pushState = function patched(...args) {
9069
- const ret = origPush.apply(this, args);
9070
- try {
9071
- dispatchRouteChange("pushState");
9072
- } catch {
9073
- }
9074
- return ret;
9075
- };
9076
- window.history.replaceState = function patched(...args) {
9077
- const ret = origReplace.apply(this, args);
9078
- try {
9079
- dispatchRouteChange("replaceState");
9080
- } catch {
9081
- }
9082
- return ret;
9083
- };
9084
- cleanups.push(() => {
9085
- window.history.pushState = origPush;
9086
- window.history.replaceState = origReplace;
9166
+ const unsubHistory = subscribeHistory({
9167
+ onPush: () => dispatchRouteChange("pushState"),
9168
+ onReplace: () => dispatchRouteChange("replaceState"),
9169
+ onPop: () => dispatchRouteChange("popstate")
9087
9170
  });
9171
+ cleanups.push(unsubHistory);
9088
9172
  } catch {
9089
9173
  }
9090
9174
  try {
9091
9175
  const origError = console.error;
9092
9176
  const origWarn = console.warn;
9093
- console.error = function(...args) {
9177
+ const errorWrapper = function(...args) {
9094
9178
  try {
9095
9179
  buffer.add({
9096
9180
  category: "console",
@@ -9101,7 +9185,7 @@ function installAutoBreadcrumbs(buffer) {
9101
9185
  }
9102
9186
  return origError.apply(this, args);
9103
9187
  };
9104
- console.warn = function(...args) {
9188
+ const warnWrapper = function(...args) {
9105
9189
  try {
9106
9190
  buffer.add({
9107
9191
  category: "console",
@@ -9112,9 +9196,11 @@ function installAutoBreadcrumbs(buffer) {
9112
9196
  }
9113
9197
  return origWarn.apply(this, args);
9114
9198
  };
9199
+ console.error = errorWrapper;
9200
+ console.warn = warnWrapper;
9115
9201
  cleanups.push(() => {
9116
- console.error = origError;
9117
- console.warn = origWarn;
9202
+ if (console.error === errorWrapper) console.error = origError;
9203
+ if (console.warn === warnWrapper) console.warn = origWarn;
9118
9204
  });
9119
9205
  } catch {
9120
9206
  }