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.
@@ -21,7 +21,7 @@ import { createMetadataMiddleware, isMetadataEnabled } from "../middlewares/crea
21
21
  import { createRouterMiddleware } from "../middlewares/createRouterMiddleware.js";
22
22
  import index from "../widgets/index/index.js";
23
23
  import createHelpers from "./createHelpers.js";
24
- import { createDocumentationMessageGenerator, createDocumentationLink, defer, hydrateSearchClient, noop, warning, setIndexHelperState, isIndexWidget } from "./utils/index.js";
24
+ import { createDocumentationMessageGenerator, createDocumentationLink, defer, hydrateRecommendCache, hydrateSearchClient, noop, warning, setIndexHelperState, isIndexWidget } from "./utils/index.js";
25
25
  import version from "./version.js";
26
26
  var withUsage = createDocumentationMessageGenerator({
27
27
  name: 'instantsearch'
@@ -459,6 +459,7 @@ var InstantSearch = /*#__PURE__*/function (_EventEmitter) {
459
459
  });
460
460
  if (this._initialResults) {
461
461
  hydrateSearchClient(this.client, this._initialResults);
462
+ hydrateRecommendCache(this.mainHelper, this._initialResults);
462
463
  var originalScheduleSearch = this.scheduleSearch;
463
464
  // We don't schedule a first search when initial results are provided
464
465
  // because we already have the results to render. This skips the initial
@@ -3,7 +3,7 @@ import type { IndexWidget, InitialResults, InstantSearch, SearchOptions } from '
3
3
  * Waits for the results from the search instance to coordinate the next steps
4
4
  * in `getServerState()`.
5
5
  */
6
- export declare function waitForResults(search: InstantSearch): Promise<SearchOptions[]>;
6
+ export declare function waitForResults(search: InstantSearch, skipRecommend?: boolean): Promise<SearchOptions[]>;
7
7
  /**
8
8
  * Walks the InstantSearch root index to construct the initial results.
9
9
  */
package/es/lib/server.js CHANGED
@@ -10,13 +10,14 @@ import { walkIndex } from "./utils/index.js";
10
10
  * in `getServerState()`.
11
11
  */
12
12
  export function waitForResults(search) {
13
+ var skipRecommend = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
13
14
  var helper = search.mainHelper;
14
15
 
15
16
  // Extract search parameters from the search client to use them
16
17
  // later during hydration.
17
18
  var requestParamsList;
18
19
  var client = helper.getClient();
19
- helper.setClient({
20
+ helper.setClient(_objectSpread(_objectSpread({}, client), {}, {
20
21
  search: function search(queries) {
21
22
  requestParamsList = queries.map(function (_ref) {
22
23
  var params = _ref.params;
@@ -24,13 +25,25 @@ export function waitForResults(search) {
24
25
  });
25
26
  return client.search(queries);
26
27
  }
27
- });
28
- helper.searchOnlyWithDerivedHelpers();
28
+ }));
29
+ search._hasSearchWidget && helper.searchOnlyWithDerivedHelpers();
30
+ !skipRecommend && search._hasRecommendWidget && helper.recommend();
29
31
  return new Promise(function (resolve, reject) {
32
+ var searchResultsReceived = !search._hasSearchWidget;
33
+ var recommendResultsReceived = !search._hasRecommendWidget || skipRecommend;
30
34
  // All derived helpers resolve in the same tick so we're safe only relying
31
35
  // on the first one.
32
36
  helper.derivedHelpers[0].on('result', function () {
33
- resolve(requestParamsList);
37
+ searchResultsReceived = true;
38
+ if (recommendResultsReceived) {
39
+ resolve(requestParamsList);
40
+ }
41
+ });
42
+ helper.derivedHelpers[0].on('recommend:result', function () {
43
+ recommendResultsReceived = true;
44
+ if (searchResultsReceived) {
45
+ resolve(requestParamsList);
46
+ }
34
47
  });
35
48
 
36
49
  // However, we listen to errors that can happen on any derived helper because
@@ -61,15 +74,21 @@ requestParamsList) {
61
74
  var initialResults = {};
62
75
  var requestParamsIndex = 0;
63
76
  walkIndex(rootIndex, function (widget) {
77
+ var _widget$getHelper;
64
78
  var searchResults = widget.getResults();
65
- if (searchResults) {
79
+ var recommendResults = (_widget$getHelper = widget.getHelper()) === null || _widget$getHelper === void 0 ? void 0 : _widget$getHelper.lastRecommendResults;
80
+ if (searchResults || recommendResults) {
66
81
  var requestParams = requestParamsList === null || requestParamsList === void 0 ? void 0 : requestParamsList[requestParamsIndex++];
67
- initialResults[widget.getIndexId()] = _objectSpread({
68
- // We convert the Helper state to a plain object to pass parsable data
69
- // structures from server to client.
82
+ initialResults[widget.getIndexId()] = _objectSpread(_objectSpread(_objectSpread({}, searchResults && {
70
83
  state: _objectSpread({}, searchResults._state),
71
84
  results: searchResults._rawResults
72
- }, requestParams && {
85
+ }), recommendResults && {
86
+ recommendResults: {
87
+ // We have to stringify + parse because of some explicitly undefined values.
88
+ params: JSON.parse(JSON.stringify(recommendResults._state.params)),
89
+ results: recommendResults._rawResults
90
+ }
91
+ }), requestParams && {
73
92
  requestParams: requestParams
74
93
  });
75
94
  }
@@ -1,2 +1,3 @@
1
1
  import type { Widget } from '../../types';
2
2
  export declare function addWidgetId(widget: Widget): void;
3
+ export declare function resetWidgetId(): void;
@@ -4,4 +4,7 @@ export function addWidgetId(widget) {
4
4
  return;
5
5
  }
6
6
  widget.$$id = id++;
7
+ }
8
+ export function resetWidgetId() {
9
+ id = 0;
7
10
  }
@@ -0,0 +1,3 @@
1
+ import type { InitialResults } from '../../types';
2
+ import type { AlgoliaSearchHelper } from 'algoliasearch-helper';
3
+ export declare function hydrateRecommendCache(helper: AlgoliaSearchHelper, initialResults: InitialResults): void;
@@ -0,0 +1,17 @@
1
+ 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); }
2
+ 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; }
3
+ 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; }
4
+ 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; }
5
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
6
+ 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); }
7
+ export function hydrateRecommendCache(helper, initialResults) {
8
+ var recommendCache = Object.keys(initialResults).reduce(function (acc, indexName) {
9
+ var initialResult = initialResults[indexName];
10
+ if (initialResult.recommendResults) {
11
+ // @MAJOR: Use `Object.assign` instead of spread operator
12
+ return _objectSpread(_objectSpread({}, acc), initialResult.recommendResults.results);
13
+ }
14
+ return acc;
15
+ }, {});
16
+ helper._recommendCache = recommendCache;
17
+ }
@@ -27,16 +27,20 @@ export function hydrateSearchClient(client, results) {
27
27
  state = _results$key.state,
28
28
  requestParams = _results$key.requestParams,
29
29
  serverResults = _results$key.results;
30
- return serverResults.map(function (result) {
30
+ return serverResults && state ? serverResults.map(function (result) {
31
31
  return _objectSpread({
32
32
  indexName: state.index || result.index
33
33
  }, requestParams || result.params ? {
34
34
  params: serializeQueryParameters(requestParams || deserializeQueryParameters(result.params))
35
35
  } : {});
36
- });
36
+ }) : [];
37
37
  });
38
38
  var cachedResults = Object.keys(results).reduce(function (acc, key) {
39
- return acc.concat(results[key].results);
39
+ var res = results[key].results;
40
+ if (!res) {
41
+ return acc;
42
+ }
43
+ return acc.concat(res);
40
44
  }, []);
41
45
 
42
46
  // Algoliasearch API Client >= v4
@@ -1,3 +1,4 @@
1
+ export * from './addWidgetId';
1
2
  export * from './capitalize';
2
3
  export * from './checkIndexUiState';
3
4
  export * from './checkRendering';
@@ -27,6 +28,7 @@ export * from './getRefinements';
27
28
  export * from './getWidgetAttribute';
28
29
  export * from './hits-absolute-position';
29
30
  export * from './hits-query-id';
31
+ export * from './hydrateRecommendCache';
30
32
  export * from './hydrateSearchClient';
31
33
  export * from './isDomElement';
32
34
  export * from './isEqual';
@@ -1,3 +1,4 @@
1
+ export * from "./addWidgetId.js";
1
2
  export * from "./capitalize.js";
2
3
  export * from "./checkIndexUiState.js";
3
4
  export * from "./checkRendering.js";
@@ -27,6 +28,7 @@ export * from "./getRefinements.js";
27
28
  export * from "./getWidgetAttribute.js";
28
29
  export * from "./hits-absolute-position.js";
29
30
  export * from "./hits-query-id.js";
31
+ export * from "./hydrateRecommendCache.js";
30
32
  export * from "./hydrateSearchClient.js";
31
33
  export * from "./isDomElement.js";
32
34
  export * from "./isEqual.js";
@@ -1,2 +1,2 @@
1
- declare const _default: "4.69.0";
1
+ declare const _default: "4.70.0";
2
2
  export default _default;
package/es/lib/version.js CHANGED
@@ -1 +1 @@
1
- export default '4.69.0';
1
+ export default '4.70.0';
@@ -1,5 +1,5 @@
1
1
  import type { SearchOptions } from './algoliasearch';
2
- import type { PlainSearchParameters, SearchForFacetValues, SearchResults } from 'algoliasearch-helper';
2
+ import type { PlainSearchParameters, RecommendParametersOptions, RecommendResults, SearchForFacetValues, SearchResults } from 'algoliasearch-helper';
3
3
  export type HitAttributeHighlightResult = {
4
4
  value: string;
5
5
  matchLevel: 'none' | 'partial' | 'full';
@@ -65,8 +65,12 @@ export type NumericRefinement = {
65
65
  };
66
66
  export type Refinement = FacetRefinement | NumericRefinement;
67
67
  type InitialResult = {
68
- state: PlainSearchParameters;
69
- results: SearchResults['_rawResults'];
68
+ state?: PlainSearchParameters;
69
+ results?: SearchResults['_rawResults'];
70
+ recommendResults?: {
71
+ params: RecommendParametersOptions['params'];
72
+ results: RecommendResults['_rawResults'];
73
+ };
70
74
  requestParams?: SearchOptions;
71
75
  };
72
76
  export type InitialResults = Record<string, InitialResult>;
@@ -396,13 +396,20 @@ var index = function index(widgetParams) {
396
396
  return _this3.getHelper().recommendState;
397
397
  });
398
398
  var indexInitialResults = (_instantSearchInstanc = instantSearchInstance._initialResults) === null || _instantSearchInstanc === void 0 ? void 0 : _instantSearchInstanc[this.getIndexId()];
399
- if (indexInitialResults) {
399
+ if (indexInitialResults !== null && indexInitialResults !== void 0 && indexInitialResults.results) {
400
400
  // We restore the shape of the results provided to the instance to respect
401
401
  // the helper's structure.
402
402
  var results = new algoliasearchHelper.SearchResults(new algoliasearchHelper.SearchParameters(indexInitialResults.state), indexInitialResults.results);
403
403
  derivedHelper.lastResults = results;
404
404
  helper.lastResults = results;
405
405
  }
406
+ if (indexInitialResults !== null && indexInitialResults !== void 0 && indexInitialResults.recommendResults) {
407
+ var recommendResults = new algoliasearchHelper.RecommendResults(new algoliasearchHelper.RecommendParameters({
408
+ params: indexInitialResults.recommendResults.params
409
+ }), indexInitialResults.recommendResults.results);
410
+ derivedHelper.lastRecommendResults = recommendResults;
411
+ helper.lastRecommendResults = recommendResults;
412
+ }
406
413
 
407
414
  // Subscribe to the Helper state changes for the page before widgets
408
415
  // are initialized. This behavior mimics the original one of the Helper.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "instantsearch.js",
3
- "version": "4.69.0",
3
+ "version": "4.70.0",
4
4
  "description": "InstantSearch.js is a JavaScript library for building performant and instant search experiences with Algolia.",
5
5
  "homepage": "https://www.algolia.com/doc/guides/building-search-ui/what-is-instantsearch/js/",
6
6
  "types": "es/index.d.ts",
@@ -31,7 +31,7 @@
31
31
  "@types/google.maps": "^3.45.3",
32
32
  "@types/hogan.js": "^3.0.0",
33
33
  "@types/qs": "^6.5.3",
34
- "algoliasearch-helper": "3.20.0",
34
+ "algoliasearch-helper": "3.21.0",
35
35
  "hogan.js": "^3.0.2",
36
36
  "htm": "^3.0.0",
37
37
  "instantsearch-ui-components": "0.6.0",
@@ -56,9 +56,9 @@
56
56
  "watch:es": "yarn --silent build:es:base --watch"
57
57
  },
58
58
  "devDependencies": {
59
- "@instantsearch/mocks": "1.44.0",
60
- "@instantsearch/tests": "1.44.0",
61
- "@instantsearch/testutils": "1.33.0",
59
+ "@instantsearch/mocks": "1.45.0",
60
+ "@instantsearch/tests": "1.45.0",
61
+ "@instantsearch/testutils": "1.34.0",
62
62
  "@storybook/html": "5.3.9",
63
63
  "@types/scriptjs": "0.0.2",
64
64
  "algoliasearch": "4.23.2",
@@ -66,5 +66,5 @@
66
66
  "scriptjs": "2.5.9",
67
67
  "webpack": "4.47.0"
68
68
  },
69
- "gitHead": "95ec6c4ce746c77eeb92fab1e42b5f833f3f04bc"
69
+ "gitHead": "5bf02d0f65e6e1fc70265319dbad660696d8ba81"
70
70
  }