@mushi-mushi/web 1.22.2 → 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.2" ;
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;
@@ -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,11 @@ 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
+ onReplace: emitRoute,
5723
+ onPop: emitRoute
5724
+ });
5636
5725
  emitRoute();
5637
5726
  routeObserver = new MutationObserver(() => emitRoute());
5638
5727
  const main = document.querySelector("main") ?? document.body;
@@ -5648,15 +5737,8 @@ function installActivityListeners(projectId) {
5648
5737
  }
5649
5738
  function removeActivityListeners() {
5650
5739
  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
- }
5740
+ historyUnsub?.();
5741
+ historyUnsub = null;
5660
5742
  routeObserver?.disconnect();
5661
5743
  routeObserver = null;
5662
5744
  if (clickHandler) {
@@ -6446,9 +6528,6 @@ function createElementSelector() {
6446
6528
  var MAX_TIMELINE_ENTRIES = 120;
6447
6529
  function createTimelineCapture() {
6448
6530
  const entries = [];
6449
- const capturedPushState = history.pushState.bind(history);
6450
- const capturedReplaceState = history.replaceState.bind(history);
6451
- const handlePopState = () => recordRoute("popstate");
6452
6531
  const handleHashChange = () => recordRoute("hashchange");
6453
6532
  recordRoute("initial");
6454
6533
  function record(entry) {
@@ -6481,19 +6560,11 @@ function createTimelineCapture() {
6481
6560
  }
6482
6561
  });
6483
6562
  }
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);
6563
+ const unsubHistory = subscribeHistory({
6564
+ onPush: () => recordRoute("pushState"),
6565
+ onReplace: () => recordRoute("replaceState"),
6566
+ onPop: () => recordRoute("popstate")
6567
+ });
6497
6568
  window.addEventListener("hashchange", handleHashChange);
6498
6569
  document.addEventListener("click", handleClick, true);
6499
6570
  return {
@@ -6533,13 +6604,7 @@ function createTimelineCapture() {
6533
6604
  entries.length = 0;
6534
6605
  },
6535
6606
  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);
6607
+ unsubHistory();
6543
6608
  window.removeEventListener("hashchange", handleHashChange);
6544
6609
  document.removeEventListener("click", handleClick, true);
6545
6610
  }
@@ -6699,32 +6764,16 @@ function createDiscoveryCapture(opts) {
6699
6764
  flushNow: () => void 0
6700
6765
  };
6701
6766
  }
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);
6767
+ const onNav = () => onMaybeNavigation();
6768
+ const unsubHistory = subscribeHistory({
6769
+ onPush: onNav,
6770
+ onReplace: onNav,
6771
+ onPop: onNav
6772
+ });
6718
6773
  scheduleEmit();
6719
6774
  return {
6720
6775
  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
- }
6776
+ unsubHistory();
6728
6777
  if (pendingTimer) {
6729
6778
  clearTimeout(pendingTimer);
6730
6779
  pendingTimer = null;
@@ -7308,28 +7357,14 @@ function setupProactiveTriggers(callbacks, config = {}) {
7308
7357
  let timer = null;
7309
7358
  let lastPath = window.location?.pathname ?? "";
7310
7359
  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);
7360
+ const unsubHistory = subscribeHistory({
7361
+ onPush: reset2,
7362
+ onReplace: reset2,
7363
+ onPop: reset2
7364
+ });
7328
7365
  cleanups.push(() => {
7329
7366
  if (timer) clearTimeout(timer);
7330
- window.removeEventListener("popstate", onPop);
7331
- if (history2 && origPush) history2.pushState = origPush;
7332
- if (history2 && origReplace) history2.replaceState = origReplace;
7367
+ unsubHistory();
7333
7368
  });
7334
7369
  }
7335
7370
  const firstSessionEnabled = config.firstSession === true || typeof config.firstSession === "object" && config.firstSession !== null;
@@ -8454,16 +8489,17 @@ function createInstance(config) {
8454
8489
  networkCap?.destroy();
8455
8490
  perfCap?.destroy();
8456
8491
  elementSelector?.deactivate();
8492
+ teardown();
8457
8493
  detachAutoBreadcrumbs?.();
8458
8494
  detachAutoBreadcrumbs = null;
8459
- timelineCap.destroy();
8460
8495
  discoveryCap?.destroy();
8461
8496
  discoveryCap = null;
8497
+ timelineCap.destroy();
8498
+ uninstallHistoryPatchForce();
8462
8499
  replayCap?.destroy();
8463
8500
  replayCap = null;
8464
8501
  offlineQueue.stopAutoSync();
8465
8502
  stopReporterInboxPolling();
8466
- teardown();
8467
8503
  breadcrumbs.clear();
8468
8504
  listeners.clear();
8469
8505
  instance = null;
@@ -9125,37 +9161,12 @@ function installAutoBreadcrumbs(buffer) {
9125
9161
  data: { url: window.location.href, kind }
9126
9162
  });
9127
9163
  };
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
- }
9164
+ const unsubHistory = subscribeHistory({
9165
+ onPush: () => dispatchRouteChange("pushState"),
9166
+ onReplace: () => dispatchRouteChange("replaceState"),
9167
+ onPop: () => dispatchRouteChange("popstate")
9158
9168
  });
9169
+ cleanups.push(unsubHistory);
9159
9170
  } catch {
9160
9171
  }
9161
9172
  try {