instantsearch.js 4.33.2 → 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 (34) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +2 -1
  3. package/cjs/connectors/autocomplete/connectAutocomplete.js +2 -1
  4. package/cjs/connectors/infinite-hits/connectInfiniteHits.js +1 -1
  5. package/cjs/connectors/pagination/connectPagination.js +3 -3
  6. package/cjs/connectors/search-box/connectSearchBox.js +4 -3
  7. package/cjs/connectors/sort-by/connectSortBy.js +2 -1
  8. package/cjs/connectors/stats/connectStats.js +4 -4
  9. package/cjs/connectors/toggle-refinement/connectToggleRefinement.js +1 -1
  10. package/cjs/lib/InstantSearch.js +22 -1
  11. package/cjs/lib/version.js +1 -1
  12. package/cjs/widgets/index/index.js +20 -2
  13. package/dist/instantsearch.development.d.ts +8 -0
  14. package/dist/instantsearch.development.js +61 -19
  15. package/dist/instantsearch.development.js.map +1 -1
  16. package/dist/instantsearch.development.min.d.ts +8 -0
  17. package/dist/instantsearch.production.d.ts +8 -0
  18. package/dist/instantsearch.production.min.d.ts +8 -0
  19. package/dist/instantsearch.production.min.js +2 -2
  20. package/dist/instantsearch.production.min.js.map +1 -1
  21. package/es/connectors/autocomplete/connectAutocomplete.js +2 -1
  22. package/es/connectors/infinite-hits/connectInfiniteHits.js +1 -1
  23. package/es/connectors/pagination/connectPagination.js +3 -3
  24. package/es/connectors/search-box/connectSearchBox.js +4 -3
  25. package/es/connectors/sort-by/connectSortBy.js +2 -1
  26. package/es/connectors/stats/connectStats.js +4 -4
  27. package/es/connectors/toggle-refinement/connectToggleRefinement.js +1 -1
  28. package/es/lib/InstantSearch.d.ts +2 -1
  29. package/es/lib/InstantSearch.js +22 -1
  30. package/es/lib/version.d.ts +1 -1
  31. package/es/lib/version.js +1 -1
  32. package/es/types/results.d.ts +7 -0
  33. package/es/widgets/index/index.js +20 -2
  34. package/package.json +2 -2
@@ -52,6 +52,7 @@ var connectAutocomplete = function connectAutocomplete(renderFn) {
52
52
  var _this = this;
53
53
 
54
54
  var helper = _ref4.helper,
55
+ state = _ref4.state,
55
56
  scopedResults = _ref4.scopedResults,
56
57
  instantSearchInstance = _ref4.instantSearchInstance;
57
58
 
@@ -79,7 +80,7 @@ var connectAutocomplete = function connectAutocomplete(renderFn) {
79
80
  };
80
81
  });
81
82
  return {
82
- currentRefinement: helper.state.query || '',
83
+ currentRefinement: state.query || '',
83
84
  indices: indices,
84
85
  refine: connectorState.refine,
85
86
  widgetParams: widgetParams
@@ -165,7 +165,7 @@ var connectInfiniteHits = function connectInfiniteHits(renderFn) {
165
165
  index: helper.getIndex(),
166
166
  widgetType: this.$$type
167
167
  });
168
- isFirstPage = helper.state.page === undefined || getFirstReceivedPage(helper.state, cachedHits) === 0;
168
+ isFirstPage = state.page === undefined || getFirstReceivedPage(state, cachedHits) === 0;
169
169
  } else {
170
170
  var _state$page3 = state.page,
171
171
  _page = _state$page3 === void 0 ? 0 : _state$page3;
@@ -78,6 +78,7 @@ var connectPagination = function connectPagination(renderFn) {
78
78
  getWidgetRenderState: function getWidgetRenderState(_ref6) {
79
79
  var results = _ref6.results,
80
80
  helper = _ref6.helper,
81
+ state = _ref6.state,
81
82
  createURL = _ref6.createURL;
82
83
 
83
84
  if (!connectorState.refine) {
@@ -88,14 +89,13 @@ var connectPagination = function connectPagination(renderFn) {
88
89
  }
89
90
 
90
91
  if (!connectorState.createURL) {
91
- connectorState.createURL = function (state) {
92
+ connectorState.createURL = function (helperState) {
92
93
  return function (page) {
93
- return createURL(state.setPage(page));
94
+ return createURL(helperState.setPage(page));
94
95
  };
95
96
  };
96
97
  }
97
98
 
98
- var state = helper.state;
99
99
  var page = state.page || 0;
100
100
  var nbPages = getMaxPage(results || {
101
101
  nbPages: 0
@@ -63,11 +63,12 @@ var connectSearchBox = function connectSearchBox(renderFn) {
63
63
  },
64
64
  getWidgetRenderState: function getWidgetRenderState(_ref3) {
65
65
  var helper = _ref3.helper,
66
- searchMetadata = _ref3.searchMetadata;
66
+ searchMetadata = _ref3.searchMetadata,
67
+ state = _ref3.state;
67
68
 
68
69
  if (!_refine) {
69
70
  var setQueryAndSearch = function setQueryAndSearch(query) {
70
- if (query !== helper.state.query) {
71
+ if (query !== state.query) {
71
72
  helper.setQuery(query).search();
72
73
  }
73
74
  };
@@ -84,7 +85,7 @@ var connectSearchBox = function connectSearchBox(renderFn) {
84
85
 
85
86
  _clear = clear(helper);
86
87
  return {
87
- query: helper.state.query || '',
88
+ query: state.query || '',
88
89
  refine: _refine,
89
90
  clear: _cachedClear,
90
91
  widgetParams: widgetParams,
@@ -64,6 +64,7 @@ var connectSortBy = function connectSortBy(renderFn) {
64
64
  getWidgetRenderState: function getWidgetRenderState(_ref3) {
65
65
  var results = _ref3.results,
66
66
  helper = _ref3.helper,
67
+ state = _ref3.state,
67
68
  parent = _ref3.parent;
68
69
 
69
70
  if (!connectorState.initialIndex && parent) {
@@ -77,7 +78,7 @@ var connectSortBy = function connectSortBy(renderFn) {
77
78
  }
78
79
 
79
80
  return {
80
- currentRefinement: helper.state.index,
81
+ currentRefinement: state.index,
81
82
  options: transformItems(items),
82
83
  refine: connectorState.setIndex,
83
84
  hasNoResults: results ? results.nbHits === 0 : true,
@@ -42,18 +42,18 @@ var connectStats = function connectStats(renderFn) {
42
42
  },
43
43
  getWidgetRenderState: function getWidgetRenderState(_ref) {
44
44
  var results = _ref.results,
45
- helper = _ref.helper;
45
+ state = _ref.state;
46
46
 
47
47
  if (!results) {
48
48
  return {
49
- hitsPerPage: helper.state.hitsPerPage,
49
+ hitsPerPage: state.hitsPerPage,
50
50
  nbHits: 0,
51
51
  nbSortedHits: undefined,
52
52
  areHitsSorted: false,
53
53
  nbPages: 0,
54
- page: helper.state.page || 0,
54
+ page: state.page || 0,
55
55
  processingTimeMS: -1,
56
- query: helper.state.query || '',
56
+ query: state.query || '',
57
57
  widgetParams: widgetParams
58
58
  };
59
59
  }
@@ -177,7 +177,7 @@ var connectToggleRefinement = function connectToggleRefinement(renderFn) {
177
177
  createURL = _ref6.createURL,
178
178
  instantSearchInstance = _ref6.instantSearchInstance;
179
179
  var isRefined = results ? on.every(function (v) {
180
- return helper.state.isDisjunctiveFacetRefined(attribute, v);
180
+ return state.isDisjunctiveFacetRefined(attribute, v);
181
181
  }) : on.every(function (v) {
182
182
  return state.isDisjunctiveFacetRefined(attribute, v);
183
183
  });
@@ -2,7 +2,7 @@
2
2
  import type { AlgoliaSearchHelper } from 'algoliasearch-helper';
3
3
  import EventEmitter from 'events';
4
4
  import type { IndexWidget } from '../widgets/index/index';
5
- import type { InsightsClient as AlgoliaInsightsClient, SearchClient, Widget, UiState, CreateURL, Middleware, MiddlewareDefinition, RenderState } from '../types';
5
+ import type { InsightsClient as AlgoliaInsightsClient, SearchClient, Widget, UiState, CreateURL, Middleware, MiddlewareDefinition, RenderState, InitialResults } from '../types';
6
6
  import type { RouterProps } from '../middlewares/createRouterMiddleware';
7
7
  import type { InsightsEvent } from '../middlewares/createInsightsMiddleware';
8
8
  /**
@@ -106,6 +106,7 @@ declare class InstantSearch<TUiState extends UiState = UiState, TRouteState = TU
106
106
  _searchStalledTimer: any;
107
107
  _isSearchStalled: boolean;
108
108
  _initialUiState: UiState;
109
+ _initialResults: InitialResults | null;
109
110
  _createURL: CreateURL<UiState>;
110
111
  _searchFunction?: InstantSearchOptions['searchFunction'];
111
112
  _mainHelperSearch?: AlgoliaSearchHelper['search'];
@@ -91,6 +91,8 @@ var InstantSearch = /*#__PURE__*/function (_EventEmitter) {
91
91
 
92
92
  _defineProperty(_assertThisInitialized(_this), "_initialUiState", void 0);
93
93
 
94
+ _defineProperty(_assertThisInitialized(_this), "_initialResults", void 0);
95
+
94
96
  _defineProperty(_assertThisInitialized(_this), "_createURL", void 0);
95
97
 
96
98
  _defineProperty(_assertThisInitialized(_this), "_searchFunction", void 0);
@@ -195,6 +197,7 @@ var InstantSearch = /*#__PURE__*/function (_EventEmitter) {
195
197
  _this._isSearchStalled = false;
196
198
  _this._createURL = defaultCreateURL;
197
199
  _this._initialUiState = initialUiState;
200
+ _this._initialResults = null;
198
201
 
199
202
  if (searchFunction) {
200
203
  _this._searchFunction = searchFunction;
@@ -437,9 +440,27 @@ var InstantSearch = /*#__PURE__*/function (_EventEmitter) {
437
440
  parent: null,
438
441
  uiState: this._initialUiState
439
442
  });
440
- this.scheduleSearch(); // Keep the previous reference for legacy purpose, some pattern use
443
+
444
+ if (this._initialResults) {
445
+ var originalScheduleSearch = this.scheduleSearch; // We don't schedule a first search when initial results are provided
446
+ // because we already have the results to render. This skips the initial
447
+ // network request on the browser on `start`.
448
+
449
+ this.scheduleSearch = defer(noop); // We also skip the initial network request when widgets are dynamically
450
+ // added in the first tick (that's the case in all the framework-based flavors).
451
+ // When we add a widget to `index`, it calls `scheduleSearch`. We can rely
452
+ // on our `defer` util to restore the original `scheduleSearch` value once
453
+ // widgets are added to hook back to the regular lifecycle.
454
+
455
+ defer(function () {
456
+ _this3.scheduleSearch = originalScheduleSearch;
457
+ })();
458
+ } else {
459
+ this.scheduleSearch();
460
+ } // Keep the previous reference for legacy purpose, some pattern use
441
461
  // the direct Helper access `search.helper` (e.g multi-index).
442
462
 
463
+
443
464
  this.helper = this.mainIndex.getHelper(); // track we started the search if we add more widgets,
444
465
  // to init them directly after add
445
466
 
@@ -1,2 +1,2 @@
1
- declare const _default: "4.33.2";
1
+ declare const _default: "4.34.0";
2
2
  export default _default;
package/es/lib/version.js CHANGED
@@ -1 +1 @@
1
- export default '4.33.2';
1
+ export default '4.34.0';
@@ -1,3 +1,4 @@
1
+ import type { PlainSearchParameters, SearchResults } from 'algoliasearch-helper';
1
2
  export declare type HitAttributeHighlightResult = {
2
3
  value: string;
3
4
  matchLevel: 'none' | 'partial' | 'full';
@@ -64,3 +65,9 @@ export declare type NumericRefinement = {
64
65
  operator: string;
65
66
  };
66
67
  export declare type Refinement = FacetRefinement | NumericRefinement;
68
+ declare type InitialResult = {
69
+ state: PlainSearchParameters;
70
+ results: SearchResults['_rawResults'];
71
+ };
72
+ export declare type InitialResults = Record<string, InitialResult>;
73
+ export {};
@@ -275,7 +275,8 @@ var index = function index(widgetParams) {
275
275
  return this;
276
276
  },
277
277
  init: function init(_ref2) {
278
- var _this3 = this;
278
+ var _this3 = this,
279
+ _instantSearchInstanc;
279
280
 
280
281
  var instantSearchInstance = _ref2.instantSearchInstance,
281
282
  parent = _ref2.parent,
@@ -333,11 +334,21 @@ var index = function index(widgetParams) {
333
334
 
334
335
  derivedHelper = mainHelper.derive(function () {
335
336
  return mergeSearchParameters.apply(void 0, _toConsumableArray(resolveSearchParameters(_this3)));
336
- }); // Subscribe to the Helper state changes for the page before widgets
337
+ });
338
+ var indexInitialResults = (_instantSearchInstanc = instantSearchInstance._initialResults) === null || _instantSearchInstanc === void 0 ? void 0 : _instantSearchInstanc[this.getIndexId()];
339
+
340
+ if (indexInitialResults) {
341
+ // We restore the shape of the results provided to the instance to respect
342
+ // the helper's structure.
343
+ var results = new algoliasearchHelper.SearchResults(new algoliasearchHelper.SearchParameters(indexInitialResults.state), indexInitialResults.results);
344
+ derivedHelper.lastResults = results;
345
+ helper.lastResults = results;
346
+ } // Subscribe to the Helper state changes for the page before widgets
337
347
  // are initialized. This behavior mimics the original one of the Helper.
338
348
  // It makes sense to replicate it at the `init` step. We have another
339
349
  // listener on `change` below, once `init` is done.
340
350
 
351
+
341
352
  helper.on('change', function (_ref3) {
342
353
  var isPageReset = _ref3.isPageReset;
343
354
 
@@ -438,6 +449,13 @@ var index = function index(widgetParams) {
438
449
  instantSearchInstance.onInternalStateChange();
439
450
  }
440
451
  });
452
+
453
+ if (indexInitialResults) {
454
+ // If there are initial results, we're not notified of the next results
455
+ // because we don't trigger an initial search. We therefore need to directly
456
+ // schedule a render that will render the results injected on the helper.
457
+ instantSearchInstance.scheduleRender();
458
+ }
441
459
  },
442
460
  render: function render(_ref5) {
443
461
  var _this4 = this;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "instantsearch.js",
3
- "version": "4.33.2",
3
+ "version": "4.34.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",
@@ -150,7 +150,7 @@
150
150
  "bundlesize": [
151
151
  {
152
152
  "path": "./dist/instantsearch.production.min.js",
153
- "maxSize": "69.25 kB"
153
+ "maxSize": "69.50 kB"
154
154
  },
155
155
  {
156
156
  "path": "./dist/instantsearch.development.js",