instantsearch.js 4.32.0 → 4.34.0

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.
Files changed (55) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/README.md +24 -1
  3. package/cjs/connectors/autocomplete/connectAutocomplete.js +2 -1
  4. package/cjs/connectors/infinite-hits/connectInfiniteHits.js +1 -1
  5. package/cjs/connectors/numeric-menu/connectNumericMenu.js +6 -4
  6. package/cjs/connectors/pagination/connectPagination.js +3 -3
  7. package/cjs/connectors/powered-by/connectPoweredBy.js +13 -4
  8. package/cjs/connectors/search-box/connectSearchBox.js +4 -3
  9. package/cjs/connectors/sort-by/connectSortBy.js +2 -1
  10. package/cjs/connectors/stats/connectStats.js +4 -4
  11. package/cjs/connectors/toggle-refinement/connectToggleRefinement.js +1 -1
  12. package/cjs/lib/InstantSearch.js +26 -3
  13. package/cjs/lib/infiniteHitsCache/sessionStorage.js +16 -12
  14. package/cjs/lib/routers/history.js +89 -42
  15. package/cjs/lib/utils/detect-insights-client.js +10 -1
  16. package/cjs/lib/utils/index.js +10 -1
  17. package/cjs/lib/utils/safelyRunOnBrowser.js +30 -0
  18. package/cjs/lib/version.js +1 -1
  19. package/cjs/lib/voiceSearchHelper/index.js +5 -0
  20. package/cjs/middlewares/createMetadataMiddleware.js +12 -3
  21. package/cjs/widgets/index/index.js +21 -3
  22. package/dist/instantsearch.development.d.ts +21 -2
  23. package/dist/instantsearch.development.js +227 -88
  24. package/dist/instantsearch.development.js.map +1 -1
  25. package/dist/instantsearch.development.min.d.ts +21 -2
  26. package/dist/instantsearch.production.d.ts +21 -2
  27. package/dist/instantsearch.production.min.d.ts +21 -2
  28. package/dist/instantsearch.production.min.js +2 -2
  29. package/dist/instantsearch.production.min.js.map +1 -1
  30. package/es/connectors/autocomplete/connectAutocomplete.js +2 -1
  31. package/es/connectors/infinite-hits/connectInfiniteHits.js +1 -1
  32. package/es/connectors/numeric-menu/connectNumericMenu.js +6 -4
  33. package/es/connectors/pagination/connectPagination.js +3 -3
  34. package/es/connectors/powered-by/connectPoweredBy.js +14 -5
  35. package/es/connectors/search-box/connectSearchBox.js +4 -3
  36. package/es/connectors/sort-by/connectSortBy.js +2 -1
  37. package/es/connectors/stats/connectStats.js +4 -4
  38. package/es/connectors/toggle-refinement/connectToggleRefinement.js +1 -1
  39. package/es/lib/InstantSearch.d.ts +2 -1
  40. package/es/lib/InstantSearch.js +26 -3
  41. package/es/lib/infiniteHitsCache/sessionStorage.js +17 -14
  42. package/es/lib/routers/history.d.ts +13 -2
  43. package/es/lib/routers/history.js +88 -42
  44. package/es/lib/utils/detect-insights-client.js +9 -1
  45. package/es/lib/utils/index.d.ts +1 -0
  46. package/es/lib/utils/index.js +2 -1
  47. package/es/lib/utils/safelyRunOnBrowser.d.ts +14 -0
  48. package/es/lib/utils/safelyRunOnBrowser.js +23 -0
  49. package/es/lib/version.d.ts +1 -1
  50. package/es/lib/version.js +1 -1
  51. package/es/lib/voiceSearchHelper/index.js +5 -0
  52. package/es/middlewares/createMetadataMiddleware.js +12 -3
  53. package/es/types/results.d.ts +7 -0
  54. package/es/widgets/index/index.js +21 -3
  55. package/package.json +5 -5
@@ -5,6 +5,15 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = hasDetectedInsightsClient;
7
7
 
8
+ var _safelyRunOnBrowser = require("./safelyRunOnBrowser");
9
+
8
10
  function hasDetectedInsightsClient() {
9
- return typeof window !== 'undefined' && Boolean(window.AlgoliaAnalyticsObject);
11
+ return (0, _safelyRunOnBrowser.safelyRunOnBrowser)(function (_ref) {
12
+ var window = _ref.window;
13
+ return Boolean(window.AlgoliaAnalyticsObject);
14
+ }, {
15
+ fallback: function fallback() {
16
+ return false;
17
+ }
18
+ });
10
19
  }
@@ -55,7 +55,8 @@ var _exportNames = {
55
55
  debounce: true,
56
56
  serializePayload: true,
57
57
  deserializePayload: true,
58
- getWidgetAttribute: true
58
+ getWidgetAttribute: true,
59
+ safelyRunOnBrowser: true
59
60
  };
60
61
  Object.defineProperty(exports, "capitalize", {
61
62
  enumerable: true,
@@ -369,6 +370,12 @@ Object.defineProperty(exports, "getWidgetAttribute", {
369
370
  return _getWidgetAttribute.getWidgetAttribute;
370
371
  }
371
372
  });
373
+ Object.defineProperty(exports, "safelyRunOnBrowser", {
374
+ enumerable: true,
375
+ get: function get() {
376
+ return _safelyRunOnBrowser.safelyRunOnBrowser;
377
+ }
378
+ });
372
379
 
373
380
  var _capitalize = _interopRequireDefault(require("./capitalize"));
374
381
 
@@ -488,4 +495,6 @@ var _serializer = require("./serializer");
488
495
 
489
496
  var _getWidgetAttribute = require("./getWidgetAttribute");
490
497
 
498
+ var _safelyRunOnBrowser = require("./safelyRunOnBrowser");
499
+
491
500
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.safelyRunOnBrowser = safelyRunOnBrowser;
7
+
8
+ // eslint-disable-next-line no-restricted-globals
9
+
10
+ /**
11
+ * Runs code on browser enviromnents safely.
12
+ */
13
+ function safelyRunOnBrowser(callback) {
14
+ var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
15
+ fallback: function fallback() {
16
+ return undefined;
17
+ }
18
+ },
19
+ fallback = _ref.fallback;
20
+
21
+ // eslint-disable-next-line no-restricted-globals
22
+ if (typeof window === 'undefined') {
23
+ return fallback();
24
+ } // eslint-disable-next-line no-restricted-globals
25
+
26
+
27
+ return callback({
28
+ window: window
29
+ });
30
+ }
@@ -4,5 +4,5 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
- var _default = '4.32.0';
7
+ var _default = '4.34.0';
8
8
  exports.default = _default;
@@ -11,6 +11,11 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
11
11
 
12
12
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
13
13
 
14
+ // `SpeechRecognition` is an API used on the browser so we can safely disable
15
+ // the `window` check.
16
+
17
+ /* eslint-disable no-restricted-globals */
18
+
14
19
  /* global SpeechRecognition SpeechRecognitionEvent */
15
20
  var createVoiceSearchHelper = function createVoiceSearchHelper(_ref) {
16
21
  var searchAsYouSpeak = _ref.searchAsYouSpeak,
@@ -6,6 +6,8 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.isMetadataEnabled = isMetadataEnabled;
7
7
  exports.createMetadataMiddleware = createMetadataMiddleware;
8
8
 
9
+ var _utils = require("../lib/utils");
10
+
9
11
  function extractPayload(widgets, instantSearchInstance, payload) {
10
12
  var parent = instantSearchInstance.mainIndex;
11
13
  var initOptions = {
@@ -51,7 +53,14 @@ function extractPayload(widgets, instantSearchInstance, payload) {
51
53
  }
52
54
 
53
55
  function isMetadataEnabled() {
54
- return typeof window !== 'undefined' && window.navigator.userAgent.indexOf('Algolia Crawler') > -1;
56
+ return (0, _utils.safelyRunOnBrowser)(function (_ref) {
57
+ var window = _ref.window;
58
+ return window.navigator.userAgent.indexOf('Algolia Crawler') > -1;
59
+ }, {
60
+ fallback: function fallback() {
61
+ return false;
62
+ }
63
+ });
55
64
  }
56
65
  /**
57
66
  * Exposes the metadata of mounted widgets in a custom
@@ -63,8 +72,8 @@ function isMetadataEnabled() {
63
72
 
64
73
 
65
74
  function createMetadataMiddleware() {
66
- return function (_ref) {
67
- var instantSearchInstance = _ref.instantSearchInstance;
75
+ return function (_ref2) {
76
+ var instantSearchInstance = _ref2.instantSearchInstance;
68
77
  var payload = {
69
78
  widgets: []
70
79
  };
@@ -289,7 +289,8 @@ var index = function index(widgetParams) {
289
289
  return this;
290
290
  },
291
291
  init: function init(_ref2) {
292
- var _this3 = this;
292
+ var _this3 = this,
293
+ _instantSearchInstanc;
293
294
 
294
295
  var instantSearchInstance = _ref2.instantSearchInstance,
295
296
  parent = _ref2.parent,
@@ -347,11 +348,21 @@ var index = function index(widgetParams) {
347
348
 
348
349
  derivedHelper = mainHelper.derive(function () {
349
350
  return _utils.mergeSearchParameters.apply(void 0, _toConsumableArray((0, _utils.resolveSearchParameters)(_this3)));
350
- }); // Subscribe to the Helper state changes for the page before widgets
351
+ });
352
+ var indexInitialResults = (_instantSearchInstanc = instantSearchInstance._initialResults) === null || _instantSearchInstanc === void 0 ? void 0 : _instantSearchInstanc[this.getIndexId()];
353
+
354
+ if (indexInitialResults) {
355
+ // We restore the shape of the results provided to the instance to respect
356
+ // the helper's structure.
357
+ var results = new _algoliasearchHelper.default.SearchResults(new _algoliasearchHelper.default.SearchParameters(indexInitialResults.state), indexInitialResults.results);
358
+ derivedHelper.lastResults = results;
359
+ helper.lastResults = results;
360
+ } // Subscribe to the Helper state changes for the page before widgets
351
361
  // are initialized. This behavior mimics the original one of the Helper.
352
362
  // It makes sense to replicate it at the `init` step. We have another
353
363
  // listener on `change` below, once `init` is done.
354
364
 
365
+
355
366
  helper.on('change', function (_ref3) {
356
367
  var isPageReset = _ref3.isPageReset;
357
368
 
@@ -452,6 +463,13 @@ var index = function index(widgetParams) {
452
463
  instantSearchInstance.onInternalStateChange();
453
464
  }
454
465
  });
466
+
467
+ if (indexInitialResults) {
468
+ // If there are initial results, we're not notified of the next results
469
+ // because we don't trigger an initial search. We therefore need to directly
470
+ // schedule a render that will render the results injected on the helper.
471
+ instantSearchInstance.scheduleRender();
472
+ }
455
473
  },
456
474
  render: function render(_ref5) {
457
475
  var _this4 = this;
@@ -555,7 +573,7 @@ var index = function index(widgetParams) {
555
573
  localUiState = getLocalWidgetsUiState(localWidgets, {
556
574
  searchParameters: this.getHelper().state,
557
575
  helper: this.getHelper()
558
- });
576
+ }, localUiState);
559
577
  }
560
578
  };
561
579
  };
@@ -455,13 +455,23 @@ declare class BrowserHistory<TRouteState> implements Router<TRouteState> {
455
455
  * It should be symmetrical to `createURL`.
456
456
  */
457
457
  private readonly parseURL;
458
+ /**
459
+ * Returns the location to store in the history.
460
+ * @default () => window.location
461
+ */
462
+ private readonly getLocation;
458
463
  private writeTimer?;
459
464
  private _onPopState;
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
468
+ */
469
+ private shouldPushState;
460
470
  /**
461
471
  * Initializes a new storage provider that syncs the search state to the URL
462
472
  * using web APIs (`window.location.pushState` and `onpopstate` event).
463
473
  */
464
- constructor({ windowTitle, writeDelay, createURL, parseURL, }: BrowserHistoryArgs<TRouteState>);
474
+ constructor({ windowTitle, writeDelay, createURL, parseURL, getLocation, }: BrowserHistoryArgs<TRouteState>);
465
475
  /**
466
476
  * Reads the URL and returns a syncable UI search state.
467
477
  */
@@ -494,6 +504,7 @@ declare type BrowserHistoryArgs<TRouteState> = {
494
504
  writeDelay: number;
495
505
  createURL: CreateURL_2<TRouteState>;
496
506
  parseURL: ParseURL<TRouteState>;
507
+ getLocation(): Location;
497
508
  };
498
509
 
499
510
  declare type BuiltInBindEventForHits = (eventType: string, hits: Hit | Hits, eventName?: string) => string;
@@ -1723,7 +1734,7 @@ declare type HighlightOptions = {
1723
1734
  }>;
1724
1735
  };
1725
1736
 
1726
- declare function historyRouter<TRouteState = UiState>({ createURL, parseURL, writeDelay, windowTitle, }?: Partial<BrowserHistoryArgs<TRouteState>>): BrowserHistory<TRouteState>;
1737
+ declare function historyRouter<TRouteState = UiState>({ createURL, parseURL, writeDelay, windowTitle, getLocation, }?: Partial<BrowserHistoryArgs<TRouteState>>): BrowserHistory<TRouteState>;
1727
1738
 
1728
1739
  declare type Hit = {
1729
1740
  __position: number;
@@ -2183,6 +2194,13 @@ declare type InfiniteHitsWidgetParams = {
2183
2194
  cache?: InfiniteHitsCache;
2184
2195
  };
2185
2196
 
2197
+ declare type InitialResult = {
2198
+ state: PlainSearchParameters;
2199
+ results: SearchResults['_rawResults'];
2200
+ };
2201
+
2202
+ declare type InitialResults = Record<string, InitialResult>;
2203
+
2186
2204
  declare type InitOptions = SharedRenderOptions & {
2187
2205
  uiState: UiState;
2188
2206
  results?: undefined;
@@ -2247,6 +2265,7 @@ declare class InstantSearch<TUiState extends UiState = UiState, TRouteState = TU
2247
2265
  _searchStalledTimer: any;
2248
2266
  _isSearchStalled: boolean;
2249
2267
  _initialUiState: UiState;
2268
+ _initialResults: InitialResults | null;
2250
2269
  _createURL: CreateURL<UiState>;
2251
2270
  _searchFunction?: InstantSearchOptions['searchFunction'];
2252
2271
  _mainHelperSearch?: AlgoliaSearchHelper['search'];