instantsearch.js 4.69.0 → 4.70.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.
@@ -467,6 +467,7 @@ var InstantSearch = /*#__PURE__*/function (_EventEmitter) {
467
467
  });
468
468
  if (this._initialResults) {
469
469
  (0, _utils.hydrateSearchClient)(this.client, this._initialResults);
470
+ (0, _utils.hydrateRecommendCache)(this.mainHelper, this._initialResults);
470
471
  var originalScheduleSearch = this.scheduleSearch;
471
472
  // We don't schedule a first search when initial results are provided
472
473
  // because we already have the results to render. This skips the initial
package/cjs/lib/server.js CHANGED
@@ -17,13 +17,14 @@ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input ==
17
17
  * in `getServerState()`.
18
18
  */
19
19
  function waitForResults(search) {
20
+ var skipRecommend = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
20
21
  var helper = search.mainHelper;
21
22
 
22
23
  // Extract search parameters from the search client to use them
23
24
  // later during hydration.
24
25
  var requestParamsList;
25
26
  var client = helper.getClient();
26
- helper.setClient({
27
+ helper.setClient(_objectSpread(_objectSpread({}, client), {}, {
27
28
  search: function search(queries) {
28
29
  requestParamsList = queries.map(function (_ref) {
29
30
  var params = _ref.params;
@@ -31,13 +32,25 @@ function waitForResults(search) {
31
32
  });
32
33
  return client.search(queries);
33
34
  }
34
- });
35
- helper.searchOnlyWithDerivedHelpers();
35
+ }));
36
+ search._hasSearchWidget && helper.searchOnlyWithDerivedHelpers();
37
+ !skipRecommend && search._hasRecommendWidget && helper.recommend();
36
38
  return new Promise(function (resolve, reject) {
39
+ var searchResultsReceived = !search._hasSearchWidget;
40
+ var recommendResultsReceived = !search._hasRecommendWidget || skipRecommend;
37
41
  // All derived helpers resolve in the same tick so we're safe only relying
38
42
  // on the first one.
39
43
  helper.derivedHelpers[0].on('result', function () {
40
- resolve(requestParamsList);
44
+ searchResultsReceived = true;
45
+ if (recommendResultsReceived) {
46
+ resolve(requestParamsList);
47
+ }
48
+ });
49
+ helper.derivedHelpers[0].on('recommend:result', function () {
50
+ recommendResultsReceived = true;
51
+ if (searchResultsReceived) {
52
+ resolve(requestParamsList);
53
+ }
41
54
  });
42
55
 
43
56
  // However, we listen to errors that can happen on any derived helper because
@@ -68,15 +81,21 @@ requestParamsList) {
68
81
  var initialResults = {};
69
82
  var requestParamsIndex = 0;
70
83
  (0, _utils.walkIndex)(rootIndex, function (widget) {
84
+ var _widget$getHelper;
71
85
  var searchResults = widget.getResults();
72
- if (searchResults) {
86
+ var recommendResults = (_widget$getHelper = widget.getHelper()) === null || _widget$getHelper === void 0 ? void 0 : _widget$getHelper.lastRecommendResults;
87
+ if (searchResults || recommendResults) {
73
88
  var requestParams = requestParamsList === null || requestParamsList === void 0 ? void 0 : requestParamsList[requestParamsIndex++];
74
- initialResults[widget.getIndexId()] = _objectSpread({
75
- // We convert the Helper state to a plain object to pass parsable data
76
- // structures from server to client.
89
+ initialResults[widget.getIndexId()] = _objectSpread(_objectSpread(_objectSpread({}, searchResults && {
77
90
  state: _objectSpread({}, searchResults._state),
78
91
  results: searchResults._rawResults
79
- }, requestParams && {
92
+ }), recommendResults && {
93
+ recommendResults: {
94
+ // We have to stringify + parse because of some explicitly undefined values.
95
+ params: JSON.parse(JSON.stringify(recommendResults._state.params)),
96
+ results: recommendResults._rawResults
97
+ }
98
+ }), requestParams && {
80
99
  requestParams: requestParams
81
100
  });
82
101
  }
@@ -4,10 +4,14 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.addWidgetId = addWidgetId;
7
+ exports.resetWidgetId = resetWidgetId;
7
8
  var id = 0;
8
9
  function addWidgetId(widget) {
9
10
  if (widget.dependsOn !== 'recommend') {
10
11
  return;
11
12
  }
12
13
  widget.$$id = id++;
14
+ }
15
+ function resetWidgetId() {
16
+ id = 0;
13
17
  }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.hydrateRecommendCache = hydrateRecommendCache;
7
+ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
8
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
9
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
10
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
11
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
12
+ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
13
+ function hydrateRecommendCache(helper, initialResults) {
14
+ var recommendCache = Object.keys(initialResults).reduce(function (acc, indexName) {
15
+ var initialResult = initialResults[indexName];
16
+ if (initialResult.recommendResults) {
17
+ // @MAJOR: Use `Object.assign` instead of spread operator
18
+ return _objectSpread(_objectSpread({}, acc), initialResult.recommendResults.results);
19
+ }
20
+ return acc;
21
+ }, {});
22
+ helper._recommendCache = recommendCache;
23
+ }
@@ -33,16 +33,20 @@ function hydrateSearchClient(client, results) {
33
33
  state = _results$key.state,
34
34
  requestParams = _results$key.requestParams,
35
35
  serverResults = _results$key.results;
36
- return serverResults.map(function (result) {
36
+ return serverResults && state ? serverResults.map(function (result) {
37
37
  return _objectSpread({
38
38
  indexName: state.index || result.index
39
39
  }, requestParams || result.params ? {
40
40
  params: serializeQueryParameters(requestParams || deserializeQueryParameters(result.params))
41
41
  } : {});
42
- });
42
+ }) : [];
43
43
  });
44
44
  var cachedResults = Object.keys(results).reduce(function (acc, key) {
45
- return acc.concat(results[key].results);
45
+ var res = results[key].results;
46
+ if (!res) {
47
+ return acc;
48
+ }
49
+ return acc.concat(res);
46
50
  }, []);
47
51
 
48
52
  // Algoliasearch API Client >= v4
@@ -3,6 +3,17 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ var _addWidgetId = require("./addWidgetId");
7
+ Object.keys(_addWidgetId).forEach(function (key) {
8
+ if (key === "default" || key === "__esModule") return;
9
+ if (key in exports && exports[key] === _addWidgetId[key]) return;
10
+ Object.defineProperty(exports, key, {
11
+ enumerable: true,
12
+ get: function get() {
13
+ return _addWidgetId[key];
14
+ }
15
+ });
16
+ });
6
17
  var _capitalize = require("./capitalize");
7
18
  Object.keys(_capitalize).forEach(function (key) {
8
19
  if (key === "default" || key === "__esModule") return;
@@ -322,6 +333,17 @@ Object.keys(_hitsQueryId).forEach(function (key) {
322
333
  }
323
334
  });
324
335
  });
336
+ var _hydrateRecommendCache = require("./hydrateRecommendCache");
337
+ Object.keys(_hydrateRecommendCache).forEach(function (key) {
338
+ if (key === "default" || key === "__esModule") return;
339
+ if (key in exports && exports[key] === _hydrateRecommendCache[key]) return;
340
+ Object.defineProperty(exports, key, {
341
+ enumerable: true,
342
+ get: function get() {
343
+ return _hydrateRecommendCache[key];
344
+ }
345
+ });
346
+ });
325
347
  var _hydrateSearchClient = require("./hydrateSearchClient");
326
348
  Object.keys(_hydrateSearchClient).forEach(function (key) {
327
349
  if (key === "default" || key === "__esModule") return;
@@ -4,5 +4,5 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
- var _default = '4.69.0';
7
+ var _default = '4.70.0';
8
8
  exports.default = _default;
@@ -403,13 +403,20 @@ var index = function index(widgetParams) {
403
403
  return _this3.getHelper().recommendState;
404
404
  });
405
405
  var indexInitialResults = (_instantSearchInstanc = instantSearchInstance._initialResults) === null || _instantSearchInstanc === void 0 ? void 0 : _instantSearchInstanc[this.getIndexId()];
406
- if (indexInitialResults) {
406
+ if (indexInitialResults !== null && indexInitialResults !== void 0 && indexInitialResults.results) {
407
407
  // We restore the shape of the results provided to the instance to respect
408
408
  // the helper's structure.
409
409
  var results = new _algoliasearchHelper.default.SearchResults(new _algoliasearchHelper.default.SearchParameters(indexInitialResults.state), indexInitialResults.results);
410
410
  derivedHelper.lastResults = results;
411
411
  helper.lastResults = results;
412
412
  }
413
+ if (indexInitialResults !== null && indexInitialResults !== void 0 && indexInitialResults.recommendResults) {
414
+ var recommendResults = new _algoliasearchHelper.default.RecommendResults(new _algoliasearchHelper.default.RecommendParameters({
415
+ params: indexInitialResults.recommendResults.params
416
+ }), indexInitialResults.recommendResults.results);
417
+ derivedHelper.lastRecommendResults = recommendResults;
418
+ helper.lastRecommendResults = recommendResults;
419
+ }
413
420
 
414
421
  // Subscribe to the Helper state changes for the page before widgets
415
422
  // are initialized. This behavior mimics the original one of the Helper.
@@ -19,7 +19,9 @@ import type { PlainSearchParameters } from 'algoliasearch-helper';
19
19
  import { default as qs_2 } from 'qs';
20
20
  import type { RecommendClassNames } from 'instantsearch-ui-components';
21
21
  import type { RecommendParameters } from 'algoliasearch-helper';
22
+ import type { RecommendParametersOptions } from 'algoliasearch-helper';
22
23
  import type { RecommendResultItem } from 'algoliasearch-helper';
24
+ import type { RecommendResults } from 'algoliasearch-helper';
23
25
  import type { RelatedProductsProps } from 'instantsearch-ui-components';
24
26
  import { SearchClient } from 'algoliasearch-helper/types/algoliasearch.js';
25
27
  import { SearchOptions } from 'algoliasearch-helper/types/algoliasearch.js';
@@ -2418,8 +2420,12 @@ declare type InfiniteHitsWidgetParams = {
2418
2420
  };
2419
2421
 
2420
2422
  declare type InitialResult = {
2421
- state: PlainSearchParameters;
2422
- results: SearchResults['_rawResults'];
2423
+ state?: PlainSearchParameters;
2424
+ results?: SearchResults['_rawResults'];
2425
+ recommendResults?: {
2426
+ params: RecommendParametersOptions['params'];
2427
+ results: RecommendResults['_rawResults'];
2428
+ };
2423
2429
  requestParams?: SearchOptions;
2424
2430
  };
2425
2431
 
@@ -1,10 +1,18 @@
1
- /*! InstantSearch.js 4.69.0 | © Algolia, Inc. and contributors; MIT License | https://github.com/algolia/instantsearch */
1
+ /*! InstantSearch.js 4.70.0 | © Algolia, Inc. and contributors; MIT License | https://github.com/algolia/instantsearch */
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) :
5
5
  (global = global || self, global.instantsearch = factory());
6
6
  }(this, (function () { 'use strict';
7
7
 
8
+ var id = 0;
9
+ function addWidgetId(widget) {
10
+ if (widget.dependsOn !== 'recommend') {
11
+ return;
12
+ }
13
+ widget.$$id = id++;
14
+ }
15
+
8
16
  function capitalize(text) {
9
17
  return text.toString().charAt(0).toUpperCase() + text.toString().slice(1);
10
18
  }
@@ -1411,6 +1419,18 @@
1411
1419
  });
1412
1420
  }
1413
1421
 
1422
+ function hydrateRecommendCache(helper, initialResults) {
1423
+ var recommendCache = Object.keys(initialResults).reduce(function (acc, indexName) {
1424
+ var initialResult = initialResults[indexName];
1425
+ if (initialResult.recommendResults) {
1426
+ // @MAJOR: Use `Object.assign` instead of spread operator
1427
+ return _objectSpread2(_objectSpread2({}, acc), initialResult.recommendResults.results);
1428
+ }
1429
+ return acc;
1430
+ }, {});
1431
+ helper._recommendCache = recommendCache;
1432
+ }
1433
+
1414
1434
  function hydrateSearchClient(client, results) {
1415
1435
  if (!results) {
1416
1436
  return;
@@ -1428,16 +1448,20 @@
1428
1448
  state = _results$key.state,
1429
1449
  requestParams = _results$key.requestParams,
1430
1450
  serverResults = _results$key.results;
1431
- return serverResults.map(function (result) {
1451
+ return serverResults && state ? serverResults.map(function (result) {
1432
1452
  return _objectSpread2({
1433
1453
  indexName: state.index || result.index
1434
1454
  }, requestParams || result.params ? {
1435
1455
  params: serializeQueryParameters(requestParams || deserializeQueryParameters(result.params))
1436
1456
  } : {});
1437
- });
1457
+ }) : [];
1438
1458
  });
1439
1459
  var cachedResults = Object.keys(results).reduce(function (acc, key) {
1440
- return acc.concat(results[key].results);
1460
+ var res = results[key].results;
1461
+ if (!res) {
1462
+ return acc;
1463
+ }
1464
+ return acc.concat(res);
1441
1465
  }, []);
1442
1466
 
1443
1467
  // Algoliasearch API Client >= v4
@@ -6523,7 +6547,7 @@
6523
6547
  **/
6524
6548
  function RecommendResults(state, results) {
6525
6549
  this._state = state;
6526
- this._rawResults = results;
6550
+ this._rawResults = {};
6527
6551
 
6528
6552
  // eslint-disable-next-line consistent-this
6529
6553
  var self = this;
@@ -6531,6 +6555,7 @@
6531
6555
  state.params.forEach(function (param) {
6532
6556
  var id = param.$$id;
6533
6557
  self[id] = results[id];
6558
+ self._rawResults[id] = results[id];
6534
6559
  });
6535
6560
  }
6536
6561
 
@@ -10446,7 +10471,7 @@
10446
10471
 
10447
10472
  var sortAndMergeRecommendations_1 = sortAndMergeRecommendations;
10448
10473
 
10449
- var version = '3.20.0';
10474
+ var version = '3.21.0';
10450
10475
 
10451
10476
  var escapeFacetValue$4 = escapeFacetValue_1.escapeFacetValue;
10452
10477
 
@@ -10587,7 +10612,7 @@
10587
10612
  this._currentNbQueries = 0;
10588
10613
  this._currentNbRecommendQueries = 0;
10589
10614
  this._searchResultsOptions = searchResultsOptions;
10590
- this.recommendCache = {};
10615
+ this._recommendCache = {};
10591
10616
  }
10592
10617
 
10593
10618
  inherits_1(AlgoliaSearchHelper, events);
@@ -12036,7 +12061,7 @@
12036
12061
  },
12037
12062
  });
12038
12063
 
12039
- var cache = this.recommendCache;
12064
+ var cache = this._recommendCache;
12040
12065
 
12041
12066
  var derivedQueries = this.derivedHelpers.map(function (derivedHelper) {
12042
12067
  var derivedIndex = derivedHelper.getModifiedState(searchState).index;
@@ -12189,7 +12214,7 @@
12189
12214
 
12190
12215
  if (this._currentNbRecommendQueries === 0) this.emit('recommendQueueEmpty');
12191
12216
 
12192
- var cache = this.recommendCache;
12217
+ var cache = this._recommendCache;
12193
12218
 
12194
12219
  var idsMap = {};
12195
12220
  ids
@@ -12523,6 +12548,13 @@
12523
12548
  */
12524
12549
  algoliasearchHelper.SearchResults = SearchResults_1;
12525
12550
 
12551
+ /**
12552
+ * Constructor for the object containing the results for Recommend.
12553
+ * @member module:algoliasearchHelper.RecommendResults
12554
+ * @type {RecommendResults}
12555
+ */
12556
+ algoliasearchHelper.RecommendResults = RecommendResults_1;
12557
+
12526
12558
  var algoliasearchHelper_1 = algoliasearchHelper;
12527
12559
 
12528
12560
  /**
@@ -15267,14 +15299,6 @@
15267
15299
  };
15268
15300
  };
15269
15301
 
15270
- var id = 0;
15271
- function addWidgetId(widget) {
15272
- if (widget.dependsOn !== 'recommend') {
15273
- return;
15274
- }
15275
- widget.$$id = id++;
15276
- }
15277
-
15278
15302
  var _excluded$7 = ["initialSearchParameters"],
15279
15303
  _excluded2$2 = ["initialRecommendParameters"];
15280
15304
  var withUsage$u = createDocumentationMessageGenerator({
@@ -15656,13 +15680,20 @@
15656
15680
  return _this3.getHelper().recommendState;
15657
15681
  });
15658
15682
  var indexInitialResults = (_instantSearchInstanc = instantSearchInstance._initialResults) === null || _instantSearchInstanc === void 0 ? void 0 : _instantSearchInstanc[this.getIndexId()];
15659
- if (indexInitialResults) {
15683
+ if (indexInitialResults !== null && indexInitialResults !== void 0 && indexInitialResults.results) {
15660
15684
  // We restore the shape of the results provided to the instance to respect
15661
15685
  // the helper's structure.
15662
15686
  var results = new algoliasearchHelper_1.SearchResults(new algoliasearchHelper_1.SearchParameters(indexInitialResults.state), indexInitialResults.results);
15663
15687
  derivedHelper.lastResults = results;
15664
15688
  helper.lastResults = results;
15665
15689
  }
15690
+ if (indexInitialResults !== null && indexInitialResults !== void 0 && indexInitialResults.recommendResults) {
15691
+ var recommendResults = new algoliasearchHelper_1.RecommendResults(new algoliasearchHelper_1.RecommendParameters({
15692
+ params: indexInitialResults.recommendResults.params
15693
+ }), indexInitialResults.recommendResults.results);
15694
+ derivedHelper.lastRecommendResults = recommendResults;
15695
+ helper.lastRecommendResults = recommendResults;
15696
+ }
15666
15697
 
15667
15698
  // Subscribe to the Helper state changes for the page before widgets
15668
15699
  // are initialized. This behavior mimics the original one of the Helper.
@@ -15947,7 +15978,7 @@
15947
15978
  };
15948
15979
  }
15949
15980
 
15950
- var version$1 = '4.69.0';
15981
+ var version$1 = '4.70.0';
15951
15982
 
15952
15983
  var withUsage$v = createDocumentationMessageGenerator({
15953
15984
  name: 'instantsearch'
@@ -16385,6 +16416,7 @@
16385
16416
  });
16386
16417
  if (this._initialResults) {
16387
16418
  hydrateSearchClient(this.client, this._initialResults);
16419
+ hydrateRecommendCache(this.mainHelper, this._initialResults);
16388
16420
  var originalScheduleSearch = this.scheduleSearch;
16389
16421
  // We don't schedule a first search when initial results are provided
16390
16422
  // because we already have the results to render. This skips the initial