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
@@ -0,0 +1,14 @@
1
+ declare type BrowserCallback<TReturn> = (params: {
2
+ window: typeof window;
3
+ }) => TReturn;
4
+ declare type SafelyRunOnBrowserOptions<TReturn> = {
5
+ /**
6
+ * Fallback to run on server environments.
7
+ */
8
+ fallback: () => TReturn;
9
+ };
10
+ /**
11
+ * Runs code on browser enviromnents safely.
12
+ */
13
+ export declare function safelyRunOnBrowser<TReturn>(callback: BrowserCallback<TReturn>, { fallback }?: SafelyRunOnBrowserOptions<TReturn>): TReturn;
14
+ export {};
@@ -0,0 +1,23 @@
1
+ // eslint-disable-next-line no-restricted-globals
2
+
3
+ /**
4
+ * Runs code on browser enviromnents safely.
5
+ */
6
+ export function safelyRunOnBrowser(callback) {
7
+ var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
8
+ fallback: function fallback() {
9
+ return undefined;
10
+ }
11
+ },
12
+ fallback = _ref.fallback;
13
+
14
+ // eslint-disable-next-line no-restricted-globals
15
+ if (typeof window === 'undefined') {
16
+ return fallback();
17
+ } // eslint-disable-next-line no-restricted-globals
18
+
19
+
20
+ return callback({
21
+ window: window
22
+ });
23
+ }
@@ -1,2 +1,2 @@
1
- declare const _default: "4.32.0";
1
+ declare const _default: "4.34.0";
2
2
  export default _default;
package/es/lib/version.js CHANGED
@@ -1 +1 @@
1
- export default '4.32.0';
1
+ export default '4.34.0';
@@ -4,6 +4,11 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
4
4
 
5
5
  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; }
6
6
 
7
+ // `SpeechRecognition` is an API used on the browser so we can safely disable
8
+ // the `window` check.
9
+
10
+ /* eslint-disable no-restricted-globals */
11
+
7
12
  /* global SpeechRecognition SpeechRecognitionEvent */
8
13
  var createVoiceSearchHelper = function createVoiceSearchHelper(_ref) {
9
14
  var searchAsYouSpeak = _ref.searchAsYouSpeak,
@@ -1,3 +1,5 @@
1
+ import { safelyRunOnBrowser } from '../lib/utils';
2
+
1
3
  function extractPayload(widgets, instantSearchInstance, payload) {
2
4
  var parent = instantSearchInstance.mainIndex;
3
5
  var initOptions = {
@@ -43,7 +45,14 @@ function extractPayload(widgets, instantSearchInstance, payload) {
43
45
  }
44
46
 
45
47
  export function isMetadataEnabled() {
46
- return typeof window !== 'undefined' && window.navigator.userAgent.indexOf('Algolia Crawler') > -1;
48
+ return safelyRunOnBrowser(function (_ref) {
49
+ var window = _ref.window;
50
+ return window.navigator.userAgent.indexOf('Algolia Crawler') > -1;
51
+ }, {
52
+ fallback: function fallback() {
53
+ return false;
54
+ }
55
+ });
47
56
  }
48
57
  /**
49
58
  * Exposes the metadata of mounted widgets in a custom
@@ -54,8 +63,8 @@ export function isMetadataEnabled() {
54
63
  */
55
64
 
56
65
  export function createMetadataMiddleware() {
57
- return function (_ref) {
58
- var instantSearchInstance = _ref.instantSearchInstance;
66
+ return function (_ref2) {
67
+ var instantSearchInstance = _ref2.instantSearchInstance;
59
68
  var payload = {
60
69
  widgets: []
61
70
  };
@@ -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;
@@ -541,7 +559,7 @@ var index = function index(widgetParams) {
541
559
  localUiState = getLocalWidgetsUiState(localWidgets, {
542
560
  searchParameters: this.getHelper().state,
543
561
  helper: this.getHelper()
544
- });
562
+ }, localUiState);
545
563
  }
546
564
  };
547
565
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "instantsearch.js",
3
- "version": "4.32.0",
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",
@@ -61,10 +61,10 @@
61
61
  "hogan.js": "^3.0.2",
62
62
  "preact": "^10.0.0",
63
63
  "qs": "^6.5.1 < 6.10",
64
- "search-insights": "^2.0.5"
64
+ "search-insights": "^2.1.0"
65
65
  },
66
66
  "devDependencies": {
67
- "@algolia/client-search": "4.10.3",
67
+ "@algolia/client-search": "4.11.0",
68
68
  "@babel/cli": "7.8.4",
69
69
  "@babel/core": "7.9.6",
70
70
  "@babel/plugin-proposal-class-properties": "7.8.3",
@@ -95,7 +95,7 @@
95
95
  "@wdio/selenium-standalone-service": "5.16.5",
96
96
  "@wdio/spec-reporter": "5.16.5",
97
97
  "@wdio/static-server-service": "5.16.5",
98
- "algoliasearch": "4.10.3",
98
+ "algoliasearch": "4.11.0",
99
99
  "algoliasearch-v3": "npm:algoliasearch@3.35.1",
100
100
  "babel-eslint": "10.0.3",
101
101
  "babel-jest": "27.1.0",
@@ -150,7 +150,7 @@
150
150
  "bundlesize": [
151
151
  {
152
152
  "path": "./dist/instantsearch.production.min.js",
153
- "maxSize": "69 kB"
153
+ "maxSize": "69.50 kB"
154
154
  },
155
155
  {
156
156
  "path": "./dist/instantsearch.development.js",