instantsearch.js 4.40.4 → 4.40.5

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/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## [4.40.5](https://github.com/algolia/instantsearch.js/compare/v4.40.4...v4.40.5) (2022-04-26)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * **routing:** prevent writing the same URL twice ([#5045](https://github.com/algolia/instantsearch.js/issues/5045)) ([5d79d92](https://github.com/algolia/instantsearch.js/commit/5d79d92b30e188e5206dcb5fe86fcac058c3f09b))
7
+
8
+
9
+
1
10
  ## [4.40.4](https://github.com/algolia/instantsearch.js/compare/v4.40.3...v4.40.4) (2022-04-13)
2
11
 
3
12
 
@@ -56,7 +56,7 @@ var BrowserHistory = /*#__PURE__*/function () {
56
56
 
57
57
  _defineProperty(this, "writeTimer", void 0);
58
58
 
59
- _defineProperty(this, "shouldPushState", true);
59
+ _defineProperty(this, "inPopState", false);
60
60
 
61
61
  _defineProperty(this, "isDisposed", false);
62
62
 
@@ -111,20 +111,14 @@ var BrowserHistory = /*#__PURE__*/function () {
111
111
  }
112
112
 
113
113
  _this2.writeTimer = setTimeout(function () {
114
- setWindowTitle(title); // We do want to `pushState` if:
115
- // - the router is not disposed, IS.js needs to update the URL
116
- // OR
117
- // - the last write was from InstantSearch.js
118
- // (unlike a SPA, where it would have last written)
114
+ setWindowTitle(title);
119
115
 
120
- var lastPushWasByISAfterDispose = !_this2.isDisposed || _this2.latestAcknowledgedHistory === window.history.length;
121
-
122
- if (_this2.shouldPushState && lastPushWasByISAfterDispose) {
116
+ if (_this2.shouldWrite(url)) {
123
117
  window.history.pushState(routeState, title || '', url);
124
118
  _this2.latestAcknowledgedHistory = window.history.length;
125
119
  }
126
120
 
127
- _this2.shouldPushState = true;
121
+ _this2.inPopState = false;
128
122
  _this2.writeTimer = undefined;
129
123
  }, _this2.writeDelay);
130
124
  });
@@ -145,7 +139,7 @@ var BrowserHistory = /*#__PURE__*/function () {
145
139
  _this3.writeTimer = undefined;
146
140
  }
147
141
 
148
- _this3.shouldPushState = false;
142
+ _this3.inPopState = true;
149
143
  var routeState = event.state; // At initial load, the state is read from the URL without update.
150
144
  // Therefore the state object is not available.
151
145
  // In this case, we fallback and read the URL.
@@ -203,18 +197,39 @@ var BrowserHistory = /*#__PURE__*/function () {
203
197
 
204
198
  this.write({});
205
199
  }
200
+ }, {
201
+ key: "shouldWrite",
202
+ value: function shouldWrite(url) {
203
+ var _this5 = this;
204
+
205
+ return (0, _index.safelyRunOnBrowser)(function (_ref6) {
206
+ var window = _ref6.window;
207
+ // We do want to `pushState` if:
208
+ // - the router is not disposed, IS.js needs to update the URL
209
+ // OR
210
+ // - the last write was from InstantSearch.js
211
+ // (unlike a SPA, where it would have last written)
212
+ var lastPushWasByISAfterDispose = !(_this5.isDisposed && _this5.latestAcknowledgedHistory !== window.history.length);
213
+ return (// When the last state change was through popstate, the IS.js state changes,
214
+ // but that should not write the URL.
215
+ !_this5.inPopState && // When the previous pushState after dispose was by IS.js, we want to write the URL.
216
+ lastPushWasByISAfterDispose && // When the URL is the same as the current one, we do not want to write it.
217
+ url !== window.location.href
218
+ );
219
+ });
220
+ }
206
221
  }]);
207
222
 
208
223
  return BrowserHistory;
209
224
  }();
210
225
 
211
226
  function historyRouter() {
212
- var _ref6 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
213
- _ref6$createURL = _ref6.createURL,
214
- createURL = _ref6$createURL === void 0 ? function (_ref7) {
215
- var qsModule = _ref7.qsModule,
216
- routeState = _ref7.routeState,
217
- location = _ref7.location;
227
+ var _ref7 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
228
+ _ref7$createURL = _ref7.createURL,
229
+ createURL = _ref7$createURL === void 0 ? function (_ref8) {
230
+ var qsModule = _ref8.qsModule,
231
+ routeState = _ref8.routeState,
232
+ location = _ref8.location;
218
233
  var protocol = location.protocol,
219
234
  hostname = location.hostname,
220
235
  _location$port = location.port,
@@ -230,11 +245,11 @@ function historyRouter() {
230
245
  }
231
246
 
232
247
  return "".concat(protocol, "//").concat(hostname).concat(portWithPrefix).concat(pathname, "?").concat(queryString).concat(hash);
233
- } : _ref6$createURL,
234
- _ref6$parseURL = _ref6.parseURL,
235
- parseURL = _ref6$parseURL === void 0 ? function (_ref8) {
236
- var qsModule = _ref8.qsModule,
237
- location = _ref8.location;
248
+ } : _ref7$createURL,
249
+ _ref7$parseURL = _ref7.parseURL,
250
+ parseURL = _ref7$parseURL === void 0 ? function (_ref9) {
251
+ var qsModule = _ref9.qsModule,
252
+ location = _ref9.location;
238
253
  // `qs` by default converts arrays with more than 20 items to an object.
239
254
  // We want to avoid this because the data structure manipulated can therefore vary.
240
255
  // Setting the limit to `100` seems a good number because the engine's default is 100
@@ -248,21 +263,21 @@ function historyRouter() {
248
263
  return qsModule.parse(location.search.slice(1), {
249
264
  arrayLimit: 99
250
265
  });
251
- } : _ref6$parseURL,
252
- _ref6$writeDelay = _ref6.writeDelay,
253
- writeDelay = _ref6$writeDelay === void 0 ? 400 : _ref6$writeDelay,
254
- windowTitle = _ref6.windowTitle,
255
- _ref6$getLocation = _ref6.getLocation,
256
- getLocation = _ref6$getLocation === void 0 ? function () {
257
- return (0, _index.safelyRunOnBrowser)(function (_ref9) {
258
- var window = _ref9.window;
266
+ } : _ref7$parseURL,
267
+ _ref7$writeDelay = _ref7.writeDelay,
268
+ writeDelay = _ref7$writeDelay === void 0 ? 400 : _ref7$writeDelay,
269
+ windowTitle = _ref7.windowTitle,
270
+ _ref7$getLocation = _ref7.getLocation,
271
+ getLocation = _ref7$getLocation === void 0 ? function () {
272
+ return (0, _index.safelyRunOnBrowser)(function (_ref10) {
273
+ var window = _ref10.window;
259
274
  return window.location;
260
275
  }, {
261
276
  fallback: function fallback() {
262
277
  throw new Error('You need to provide `getLocation` to the `history` router in environments where `window` does not exist.');
263
278
  }
264
279
  });
265
- } : _ref6$getLocation;
280
+ } : _ref7$getLocation;
266
281
 
267
282
  return new BrowserHistory({
268
283
  createURL: createURL,
@@ -4,5 +4,5 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
- var _default = '4.40.4';
7
+ var _default = '4.40.5';
8
8
  exports.default = _default;
@@ -463,10 +463,9 @@ declare class BrowserHistory<TRouteState> implements Router<TRouteState> {
463
463
  private writeTimer?;
464
464
  private _onPopState;
465
465
  /**
466
- * Indicates if history.pushState should be executed.
467
- * It needs to avoid pushing state to history in case of back/forward in browser
466
+ * Indicates if last action was back/forward in the browser.
468
467
  */
469
- private shouldPushState;
468
+ private inPopState;
470
469
  /**
471
470
  * Indicates whether the history router is disposed or not.
472
471
  */
@@ -508,6 +507,7 @@ declare class BrowserHistory<TRouteState> implements Router<TRouteState> {
508
507
  * Removes the event listener and cleans up the URL.
509
508
  */
510
509
  dispose(): void;
510
+ private shouldWrite;
511
511
  }
512
512
 
513
513
  declare type BrowserHistoryArgs<TRouteState> = {
@@ -1,4 +1,4 @@
1
- /*! InstantSearch.js 4.40.4 | © Algolia, Inc. and contributors; MIT License | https://github.com/algolia/instantsearch.js */
1
+ /*! InstantSearch.js 4.40.5 | © Algolia, Inc. and contributors; MIT License | https://github.com/algolia/instantsearch.js */
2
2
  (function (global, factory) {
3
3
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
4
4
  typeof define === 'function' && define.amd ? define(factory) :
@@ -8536,7 +8536,7 @@
8536
8536
  instantSearchInstance.renderState = _objectSpread2(_objectSpread2({}, instantSearchInstance.renderState), {}, _defineProperty({}, parentIndexName, _objectSpread2(_objectSpread2({}, instantSearchInstance.renderState[parentIndexName]), renderState)));
8537
8537
  }
8538
8538
 
8539
- var version$1 = '4.40.4';
8539
+ var version$1 = '4.40.5';
8540
8540
 
8541
8541
  var NAMESPACE = 'ais';
8542
8542
  var component = function component(componentName) {
@@ -9651,7 +9651,7 @@
9651
9651
 
9652
9652
  _defineProperty(this, "writeTimer", void 0);
9653
9653
 
9654
- _defineProperty(this, "shouldPushState", true);
9654
+ _defineProperty(this, "inPopState", false);
9655
9655
 
9656
9656
  _defineProperty(this, "isDisposed", false);
9657
9657
 
@@ -9706,20 +9706,14 @@
9706
9706
  }
9707
9707
 
9708
9708
  _this2.writeTimer = setTimeout(function () {
9709
- setWindowTitle(title); // We do want to `pushState` if:
9710
- // - the router is not disposed, IS.js needs to update the URL
9711
- // OR
9712
- // - the last write was from InstantSearch.js
9713
- // (unlike a SPA, where it would have last written)
9709
+ setWindowTitle(title);
9714
9710
 
9715
- var lastPushWasByISAfterDispose = !_this2.isDisposed || _this2.latestAcknowledgedHistory === window.history.length;
9716
-
9717
- if (_this2.shouldPushState && lastPushWasByISAfterDispose) {
9711
+ if (_this2.shouldWrite(url)) {
9718
9712
  window.history.pushState(routeState, title || '', url);
9719
9713
  _this2.latestAcknowledgedHistory = window.history.length;
9720
9714
  }
9721
9715
 
9722
- _this2.shouldPushState = true;
9716
+ _this2.inPopState = false;
9723
9717
  _this2.writeTimer = undefined;
9724
9718
  }, _this2.writeDelay);
9725
9719
  });
@@ -9740,7 +9734,7 @@
9740
9734
  _this3.writeTimer = undefined;
9741
9735
  }
9742
9736
 
9743
- _this3.shouldPushState = false;
9737
+ _this3.inPopState = true;
9744
9738
  var routeState = event.state; // At initial load, the state is read from the URL without update.
9745
9739
  // Therefore the state object is not available.
9746
9740
  // In this case, we fallback and read the URL.
@@ -9798,18 +9792,39 @@
9798
9792
 
9799
9793
  this.write({});
9800
9794
  }
9795
+ }, {
9796
+ key: "shouldWrite",
9797
+ value: function shouldWrite(url) {
9798
+ var _this5 = this;
9799
+
9800
+ return safelyRunOnBrowser(function (_ref6) {
9801
+ var window = _ref6.window;
9802
+ // We do want to `pushState` if:
9803
+ // - the router is not disposed, IS.js needs to update the URL
9804
+ // OR
9805
+ // - the last write was from InstantSearch.js
9806
+ // (unlike a SPA, where it would have last written)
9807
+ var lastPushWasByISAfterDispose = !(_this5.isDisposed && _this5.latestAcknowledgedHistory !== window.history.length);
9808
+ return (// When the last state change was through popstate, the IS.js state changes,
9809
+ // but that should not write the URL.
9810
+ !_this5.inPopState && // When the previous pushState after dispose was by IS.js, we want to write the URL.
9811
+ lastPushWasByISAfterDispose && // When the URL is the same as the current one, we do not want to write it.
9812
+ url !== window.location.href
9813
+ );
9814
+ });
9815
+ }
9801
9816
  }]);
9802
9817
 
9803
9818
  return BrowserHistory;
9804
9819
  }();
9805
9820
 
9806
9821
  function historyRouter() {
9807
- var _ref6 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
9808
- _ref6$createURL = _ref6.createURL,
9809
- createURL = _ref6$createURL === void 0 ? function (_ref7) {
9810
- var qsModule = _ref7.qsModule,
9811
- routeState = _ref7.routeState,
9812
- location = _ref7.location;
9822
+ var _ref7 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
9823
+ _ref7$createURL = _ref7.createURL,
9824
+ createURL = _ref7$createURL === void 0 ? function (_ref8) {
9825
+ var qsModule = _ref8.qsModule,
9826
+ routeState = _ref8.routeState,
9827
+ location = _ref8.location;
9813
9828
  var protocol = location.protocol,
9814
9829
  hostname = location.hostname,
9815
9830
  _location$port = location.port,
@@ -9825,11 +9840,11 @@
9825
9840
  }
9826
9841
 
9827
9842
  return "".concat(protocol, "//").concat(hostname).concat(portWithPrefix).concat(pathname, "?").concat(queryString).concat(hash);
9828
- } : _ref6$createURL,
9829
- _ref6$parseURL = _ref6.parseURL,
9830
- parseURL = _ref6$parseURL === void 0 ? function (_ref8) {
9831
- var qsModule = _ref8.qsModule,
9832
- location = _ref8.location;
9843
+ } : _ref7$createURL,
9844
+ _ref7$parseURL = _ref7.parseURL,
9845
+ parseURL = _ref7$parseURL === void 0 ? function (_ref9) {
9846
+ var qsModule = _ref9.qsModule,
9847
+ location = _ref9.location;
9833
9848
  // `qs` by default converts arrays with more than 20 items to an object.
9834
9849
  // We want to avoid this because the data structure manipulated can therefore vary.
9835
9850
  // Setting the limit to `100` seems a good number because the engine's default is 100
@@ -9843,21 +9858,21 @@
9843
9858
  return qsModule.parse(location.search.slice(1), {
9844
9859
  arrayLimit: 99
9845
9860
  });
9846
- } : _ref6$parseURL,
9847
- _ref6$writeDelay = _ref6.writeDelay,
9848
- writeDelay = _ref6$writeDelay === void 0 ? 400 : _ref6$writeDelay,
9849
- windowTitle = _ref6.windowTitle,
9850
- _ref6$getLocation = _ref6.getLocation,
9851
- getLocation = _ref6$getLocation === void 0 ? function () {
9852
- return safelyRunOnBrowser(function (_ref9) {
9853
- var window = _ref9.window;
9861
+ } : _ref7$parseURL,
9862
+ _ref7$writeDelay = _ref7.writeDelay,
9863
+ writeDelay = _ref7$writeDelay === void 0 ? 400 : _ref7$writeDelay,
9864
+ windowTitle = _ref7.windowTitle,
9865
+ _ref7$getLocation = _ref7.getLocation,
9866
+ getLocation = _ref7$getLocation === void 0 ? function () {
9867
+ return safelyRunOnBrowser(function (_ref10) {
9868
+ var window = _ref10.window;
9854
9869
  return window.location;
9855
9870
  }, {
9856
9871
  fallback: function fallback() {
9857
9872
  throw new Error('You need to provide `getLocation` to the `history` router in environments where `window` does not exist.');
9858
9873
  }
9859
9874
  });
9860
- } : _ref6$getLocation;
9875
+ } : _ref7$getLocation;
9861
9876
 
9862
9877
  return new BrowserHistory({
9863
9878
  createURL: createURL,