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