@mushi-mushi/web 1.22.2 → 1.22.4

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.2" ;
2554
+ var MUSHI_SDK_VERSION = "1.22.4" ;
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;
@@ -5601,13 +5696,11 @@ function teardown() {
5601
5696
  }
5602
5697
  var routeObserver = null;
5603
5698
  var clickHandler = null;
5604
- var popstateHandler = null;
5605
- var origPushState = null;
5606
- var pushStateWrapper = null;
5699
+ var historyUnsub = null;
5607
5700
  var lastRoute = "";
5608
5701
  var listenersInstalled = false;
5609
5702
  function installActivityListeners(projectId) {
5610
- if (listenersInstalled && pushStateWrapper && history.pushState === pushStateWrapper) {
5703
+ if (listenersInstalled && historyUnsub) {
5611
5704
  return;
5612
5705
  }
5613
5706
  if (listenersInstalled) {
@@ -5624,15 +5717,10 @@ function installActivityListeners(projectId) {
5624
5717
  enqueue({ action: "screen_view_unique_per_day", metadata: { route } });
5625
5718
  }
5626
5719
  };
5627
- const capturedPushState = history.pushState.bind(history);
5628
- origPushState = capturedPushState;
5629
- pushStateWrapper = function(...args) {
5630
- capturedPushState(...args);
5631
- emitRoute();
5632
- };
5633
- history.pushState = pushStateWrapper;
5634
- popstateHandler = emitRoute;
5635
- window.addEventListener("popstate", emitRoute);
5720
+ historyUnsub = subscribeHistory({
5721
+ onPush: emitRoute,
5722
+ onPop: emitRoute
5723
+ });
5636
5724
  emitRoute();
5637
5725
  routeObserver = new MutationObserver(() => emitRoute());
5638
5726
  const main = document.querySelector("main") ?? document.body;
@@ -5648,15 +5736,8 @@ function installActivityListeners(projectId) {
5648
5736
  }
5649
5737
  function removeActivityListeners() {
5650
5738
  listenersInstalled = false;
5651
- if (origPushState && history.pushState === pushStateWrapper) {
5652
- history.pushState = origPushState;
5653
- }
5654
- origPushState = null;
5655
- pushStateWrapper = null;
5656
- if (popstateHandler) {
5657
- window.removeEventListener("popstate", popstateHandler);
5658
- popstateHandler = null;
5659
- }
5739
+ historyUnsub?.();
5740
+ historyUnsub = null;
5660
5741
  routeObserver?.disconnect();
5661
5742
  routeObserver = null;
5662
5743
  if (clickHandler) {
@@ -6446,9 +6527,6 @@ function createElementSelector() {
6446
6527
  var MAX_TIMELINE_ENTRIES = 120;
6447
6528
  function createTimelineCapture() {
6448
6529
  const entries = [];
6449
- const capturedPushState = history.pushState.bind(history);
6450
- const capturedReplaceState = history.replaceState.bind(history);
6451
- const handlePopState = () => recordRoute("popstate");
6452
6530
  const handleHashChange = () => recordRoute("hashchange");
6453
6531
  recordRoute("initial");
6454
6532
  function record(entry) {
@@ -6481,19 +6559,11 @@ function createTimelineCapture() {
6481
6559
  }
6482
6560
  });
6483
6561
  }
6484
- const pushStateWrapper2 = function mushiPushState(...args) {
6485
- const result = capturedPushState(...args);
6486
- recordRoute("pushState");
6487
- return result;
6488
- };
6489
- const replaceStateWrapper = function mushiReplaceState(...args) {
6490
- const result = capturedReplaceState(...args);
6491
- recordRoute("replaceState");
6492
- return result;
6493
- };
6494
- history.pushState = pushStateWrapper2;
6495
- history.replaceState = replaceStateWrapper;
6496
- window.addEventListener("popstate", handlePopState);
6562
+ const unsubHistory = subscribeHistory({
6563
+ onPush: () => recordRoute("pushState"),
6564
+ onReplace: () => recordRoute("replaceState"),
6565
+ onPop: () => recordRoute("popstate")
6566
+ });
6497
6567
  window.addEventListener("hashchange", handleHashChange);
6498
6568
  document.addEventListener("click", handleClick, true);
6499
6569
  return {
@@ -6533,13 +6603,7 @@ function createTimelineCapture() {
6533
6603
  entries.length = 0;
6534
6604
  },
6535
6605
  destroy() {
6536
- if (history.pushState === pushStateWrapper2) {
6537
- history.pushState = capturedPushState;
6538
- }
6539
- if (history.replaceState === replaceStateWrapper) {
6540
- history.replaceState = capturedReplaceState;
6541
- }
6542
- window.removeEventListener("popstate", handlePopState);
6606
+ unsubHistory();
6543
6607
  window.removeEventListener("hashchange", handleHashChange);
6544
6608
  document.removeEventListener("click", handleClick, true);
6545
6609
  }
@@ -6699,32 +6763,16 @@ function createDiscoveryCapture(opts) {
6699
6763
  flushNow: () => void 0
6700
6764
  };
6701
6765
  }
6702
- const originalPush = window.history.pushState.bind(window.history);
6703
- const originalReplace = window.history.replaceState.bind(window.history);
6704
- const patchedPush = function patched(...args) {
6705
- const out = originalPush(...args);
6706
- onMaybeNavigation();
6707
- return out;
6708
- };
6709
- const patchedReplace = function patched(...args) {
6710
- const out = originalReplace(...args);
6711
- onMaybeNavigation();
6712
- return out;
6713
- };
6714
- window.history.pushState = patchedPush;
6715
- window.history.replaceState = patchedReplace;
6716
- const onPop = () => onMaybeNavigation();
6717
- window.addEventListener("popstate", onPop);
6766
+ const onNav = () => onMaybeNavigation();
6767
+ const unsubHistory = subscribeHistory({
6768
+ onPush: onNav,
6769
+ onReplace: onNav,
6770
+ onPop: onNav
6771
+ });
6718
6772
  scheduleEmit();
6719
6773
  return {
6720
6774
  destroy() {
6721
- window.removeEventListener("popstate", onPop);
6722
- if (window.history.pushState === patchedPush) {
6723
- window.history.pushState = originalPush;
6724
- }
6725
- if (window.history.replaceState === patchedReplace) {
6726
- window.history.replaceState = originalReplace;
6727
- }
6775
+ unsubHistory();
6728
6776
  if (pendingTimer) {
6729
6777
  clearTimeout(pendingTimer);
6730
6778
  pendingTimer = null;
@@ -7308,28 +7356,14 @@ function setupProactiveTriggers(callbacks, config = {}) {
7308
7356
  let timer = null;
7309
7357
  let lastPath = window.location?.pathname ?? "";
7310
7358
  arm2();
7311
- const history2 = window.history;
7312
- const origPush = history2?.pushState;
7313
- const origReplace = history2?.replaceState;
7314
- if (history2 && origPush && origReplace) {
7315
- history2.pushState = function(...args) {
7316
- const result = origPush.apply(this, args);
7317
- reset2();
7318
- return result;
7319
- };
7320
- history2.replaceState = function(...args) {
7321
- const result = origReplace.apply(this, args);
7322
- reset2();
7323
- return result;
7324
- };
7325
- }
7326
- const onPop = () => reset2();
7327
- window.addEventListener("popstate", onPop);
7359
+ const unsubHistory = subscribeHistory({
7360
+ onPush: reset2,
7361
+ onReplace: reset2,
7362
+ onPop: reset2
7363
+ });
7328
7364
  cleanups.push(() => {
7329
7365
  if (timer) clearTimeout(timer);
7330
- window.removeEventListener("popstate", onPop);
7331
- if (history2 && origPush) history2.pushState = origPush;
7332
- if (history2 && origReplace) history2.replaceState = origReplace;
7366
+ unsubHistory();
7333
7367
  });
7334
7368
  }
7335
7369
  const firstSessionEnabled = config.firstSession === true || typeof config.firstSession === "object" && config.firstSession !== null;
@@ -8454,16 +8488,17 @@ function createInstance(config) {
8454
8488
  networkCap?.destroy();
8455
8489
  perfCap?.destroy();
8456
8490
  elementSelector?.deactivate();
8491
+ teardown();
8457
8492
  detachAutoBreadcrumbs?.();
8458
8493
  detachAutoBreadcrumbs = null;
8459
- timelineCap.destroy();
8460
8494
  discoveryCap?.destroy();
8461
8495
  discoveryCap = null;
8496
+ timelineCap.destroy();
8497
+ uninstallHistoryPatchForce();
8462
8498
  replayCap?.destroy();
8463
8499
  replayCap = null;
8464
8500
  offlineQueue.stopAutoSync();
8465
8501
  stopReporterInboxPolling();
8466
- teardown();
8467
8502
  breadcrumbs.clear();
8468
8503
  listeners.clear();
8469
8504
  instance = null;
@@ -9125,37 +9160,12 @@ function installAutoBreadcrumbs(buffer) {
9125
9160
  data: { url: window.location.href, kind }
9126
9161
  });
9127
9162
  };
9128
- const onPop = () => dispatchRouteChange("popstate");
9129
- window.addEventListener("popstate", onPop, { passive: true });
9130
- cleanups.push(() => window.removeEventListener("popstate", onPop));
9131
- const capturedPush = window.history.pushState.bind(window.history);
9132
- const capturedReplace = window.history.replaceState.bind(window.history);
9133
- const pushPatched = function patched(...args) {
9134
- const ret = capturedPush(...args);
9135
- try {
9136
- dispatchRouteChange("pushState");
9137
- } catch {
9138
- }
9139
- return ret;
9140
- };
9141
- const replacePatched = function patched(...args) {
9142
- const ret = capturedReplace(...args);
9143
- try {
9144
- dispatchRouteChange("replaceState");
9145
- } catch {
9146
- }
9147
- return ret;
9148
- };
9149
- window.history.pushState = pushPatched;
9150
- window.history.replaceState = replacePatched;
9151
- cleanups.push(() => {
9152
- if (window.history.pushState === pushPatched) {
9153
- window.history.pushState = capturedPush;
9154
- }
9155
- if (window.history.replaceState === replacePatched) {
9156
- window.history.replaceState = capturedReplace;
9157
- }
9163
+ const unsubHistory = subscribeHistory({
9164
+ onPush: () => dispatchRouteChange("pushState"),
9165
+ onReplace: () => dispatchRouteChange("replaceState"),
9166
+ onPop: () => dispatchRouteChange("popstate")
9158
9167
  });
9168
+ cleanups.push(unsubHistory);
9159
9169
  } catch {
9160
9170
  }
9161
9171
  try {