instantsearch.js 4.31.0 → 4.33.1

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 (45) hide show
  1. package/CHANGELOG.md +42 -0
  2. package/cjs/connectors/powered-by/connectPoweredBy.js +13 -4
  3. package/cjs/lib/InstantSearch.js +4 -2
  4. package/cjs/lib/infiniteHitsCache/sessionStorage.js +16 -12
  5. package/cjs/lib/insights/client.js +6 -1
  6. package/cjs/lib/routers/history.js +89 -42
  7. package/cjs/lib/utils/detect-insights-client.js +10 -1
  8. package/cjs/lib/utils/getWidgetAttribute.js +18 -8
  9. package/cjs/lib/utils/index.js +10 -1
  10. package/cjs/lib/utils/safelyRunOnBrowser.js +30 -0
  11. package/cjs/lib/version.js +1 -1
  12. package/cjs/lib/voiceSearchHelper/index.js +5 -0
  13. package/cjs/middlewares/createMetadataMiddleware.js +13 -5
  14. package/cjs/widgets/index/index.js +1 -1
  15. package/dist/instantsearch.development.d.ts +32 -26
  16. package/dist/instantsearch.development.js +213 -84
  17. package/dist/instantsearch.development.js.map +1 -1
  18. package/dist/instantsearch.development.min.d.ts +32 -26
  19. package/dist/instantsearch.production.d.ts +32 -26
  20. package/dist/instantsearch.production.min.d.ts +32 -26
  21. package/dist/instantsearch.production.min.js +2 -2
  22. package/dist/instantsearch.production.min.js.map +1 -1
  23. package/es/connectors/powered-by/connectPoweredBy.js +14 -5
  24. package/es/lib/InstantSearch.js +4 -2
  25. package/es/lib/infiniteHitsCache/sessionStorage.js +17 -14
  26. package/es/lib/insights/client.js +6 -1
  27. package/es/lib/insights/listener.d.ts +2 -2
  28. package/es/lib/routers/history.d.ts +13 -2
  29. package/es/lib/routers/history.js +88 -42
  30. package/es/lib/utils/detect-insights-client.js +9 -1
  31. package/es/lib/utils/getWidgetAttribute.js +19 -9
  32. package/es/lib/utils/index.d.ts +1 -0
  33. package/es/lib/utils/index.js +2 -1
  34. package/es/lib/utils/safelyRunOnBrowser.d.ts +14 -0
  35. package/es/lib/utils/safelyRunOnBrowser.js +23 -0
  36. package/es/lib/version.d.ts +1 -1
  37. package/es/lib/version.js +1 -1
  38. package/es/lib/voiceSearchHelper/index.js +5 -0
  39. package/es/middlewares/createMetadataMiddleware.js +13 -5
  40. package/es/types/connector.d.ts +2 -2
  41. package/es/types/insights.d.ts +12 -15
  42. package/es/types/widget-factory.d.ts +2 -2
  43. package/es/types/widget.d.ts +2 -2
  44. package/es/widgets/index/index.js +1 -1
  45. package/package.json +5 -5
package/CHANGELOG.md CHANGED
@@ -1,3 +1,45 @@
1
+ ## [4.33.1](https://github.com/algolia/instantsearch.js/compare/v4.33.0...v4.33.1) (2021-11-02)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * **getUiState:** support `initialUiState` ([#4948](https://github.com/algolia/instantsearch.js/issues/4948)) ([532474d](https://github.com/algolia/instantsearch.js/commit/532474dfaf49446ab59a2a27424ca220947dd5bd))
7
+
8
+
9
+
10
+ # [4.33.0](https://github.com/algolia/instantsearch.js/compare/v4.32.0...v4.33.0) (2021-10-26)
11
+
12
+
13
+ ### Bug Fixes
14
+
15
+ * **router:** skip history push on browser back and forward actions ([#4933](https://github.com/algolia/instantsearch.js/issues/4933)) ([7909da4](https://github.com/algolia/instantsearch.js/commit/7909da4903eb1aee885811e280b909a3bda488be))
16
+ * **setUiState:** reset UI state with empty object ([#4944](https://github.com/algolia/instantsearch.js/issues/4944)) ([5faae4a](https://github.com/algolia/instantsearch.js/commit/5faae4ac44ac5ad2f8086ad2a306bcfaa14bc754))
17
+
18
+
19
+ ### Features
20
+
21
+ * **router:** support server environments ([#4940](https://github.com/algolia/instantsearch.js/issues/4940)) ([a002962](https://github.com/algolia/instantsearch.js/commit/a002962df0e7683b29bef8bfaaddb494fa551a14))
22
+
23
+
24
+
25
+ # [4.32.0](https://github.com/algolia/instantsearch.js/compare/v4.31.1...v4.32.0) (2021-10-20)
26
+
27
+
28
+ ### Features
29
+
30
+ * **dependencies:** update algoliasearch-helper ([#4936](https://github.com/algolia/instantsearch.js/issues/4936)) ([014a413](https://github.com/algolia/instantsearch.js/commit/014a413f14dded3861a9c288ea618f1602bcd66d))
31
+
32
+
33
+
34
+ ## [4.31.1](https://github.com/algolia/instantsearch.js/compare/v4.31.0...v4.31.1) (2021-10-19)
35
+
36
+
37
+ ### Bug Fixes
38
+
39
+ * **types:** export correct types from search-insights ([#4930](https://github.com/algolia/instantsearch.js/issues/4930)) ([5ae7a5b](https://github.com/algolia/instantsearch.js/commit/5ae7a5b86ad9c042bfbdc60e505c159eebdb404f))
40
+
41
+
42
+
1
43
  # [4.31.0](https://github.com/algolia/instantsearch.js/compare/v4.30.3...v4.31.0) (2021-10-14)
2
44
 
3
45
 
@@ -25,11 +25,20 @@ var withUsage = (0, _utils.createDocumentationMessageGenerator)({
25
25
  var connectPoweredBy = function connectPoweredBy(renderFn) {
26
26
  var unmountFn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _utils.noop;
27
27
  (0, _utils.checkRendering)(renderFn, withUsage());
28
- var defaultUrl = 'https://www.algolia.com/?' + 'utm_source=instantsearch.js&' + 'utm_medium=website&' + "utm_content=".concat(typeof window !== 'undefined' && window.location ? window.location.hostname : '', "&") + 'utm_campaign=poweredby';
28
+ var defaultUrl = 'https://www.algolia.com/?' + 'utm_source=instantsearch.js&' + 'utm_medium=website&' + "utm_content=".concat((0, _utils.safelyRunOnBrowser)(function (_ref) {
29
+ var _window$location;
30
+
31
+ var window = _ref.window;
32
+ return ((_window$location = window.location) === null || _window$location === void 0 ? void 0 : _window$location.hostname) || '';
33
+ }, {
34
+ fallback: function fallback() {
35
+ return '';
36
+ }
37
+ }), "&") + 'utm_campaign=poweredby';
29
38
  return function (widgetParams) {
30
- var _ref = widgetParams || {},
31
- _ref$url = _ref.url,
32
- url = _ref$url === void 0 ? defaultUrl : _ref$url;
39
+ var _ref2 = widgetParams || {},
40
+ _ref2$url = _ref2.url,
41
+ url = _ref2$url === void 0 ? defaultUrl : _ref2$url;
33
42
 
34
43
  return {
35
44
  $$type: 'ais.poweredBy',
@@ -522,15 +522,17 @@ var InstantSearch = /*#__PURE__*/function (_EventEmitter) {
522
522
  var nextUiState = typeof uiState === 'function' ? uiState(this.mainIndex.getWidgetUiState({})) : uiState;
523
523
 
524
524
  var setIndexHelperState = function setIndexHelperState(indexWidget) {
525
+ var nextIndexUiState = nextUiState[indexWidget.getIndexId()] || {};
526
+
525
527
  if (process.env.NODE_ENV === 'development') {
526
528
  (0, _utils.checkIndexUiState)({
527
529
  index: indexWidget,
528
- indexUiState: nextUiState[indexWidget.getIndexId()]
530
+ indexUiState: nextIndexUiState
529
531
  });
530
532
  }
531
533
 
532
534
  indexWidget.getHelper().setState(indexWidget.getWidgetSearchParameters(indexWidget.getHelper().state, {
533
- uiState: nextUiState[indexWidget.getIndexId()]
535
+ uiState: nextIndexUiState
534
536
  }));
535
537
  indexWidget.getWidgets().filter(_index.isIndexWidget).forEach(setIndexHelperState);
536
538
  };
@@ -21,27 +21,27 @@ function getStateWithoutPage(state) {
21
21
 
22
22
  var KEY = 'ais.infiniteHits';
23
23
 
24
- function hasSessionStorage() {
25
- return typeof window !== 'undefined' && typeof window.sessionStorage !== 'undefined';
26
- }
27
-
28
24
  function createInfiniteHitsSessionStorageCache() {
29
25
  return {
30
26
  read: function read(_ref2) {
31
27
  var state = _ref2.state;
28
+ var sessionStorage = (0, _utils.safelyRunOnBrowser)(function (_ref3) {
29
+ var window = _ref3.window;
30
+ return window.sessionStorage;
31
+ });
32
32
 
33
- if (!hasSessionStorage()) {
33
+ if (!sessionStorage) {
34
34
  return null;
35
35
  }
36
36
 
37
37
  try {
38
38
  var cache = JSON.parse( // @ts-expect-error JSON.parse() requires a string, but it actually accepts null, too.
39
- window.sessionStorage.getItem(KEY));
39
+ sessionStorage.getItem(KEY));
40
40
  return cache && (0, _utils.isEqual)(cache.state, getStateWithoutPage(state)) ? cache.hits : null;
41
41
  } catch (error) {
42
42
  if (error instanceof SyntaxError) {
43
43
  try {
44
- window.sessionStorage.removeItem(KEY);
44
+ sessionStorage.removeItem(KEY);
45
45
  } catch (err) {// do nothing
46
46
  }
47
47
  }
@@ -49,16 +49,20 @@ function createInfiniteHitsSessionStorageCache() {
49
49
  return null;
50
50
  }
51
51
  },
52
- write: function write(_ref3) {
53
- var state = _ref3.state,
54
- hits = _ref3.hits;
52
+ write: function write(_ref4) {
53
+ var state = _ref4.state,
54
+ hits = _ref4.hits;
55
+ var sessionStorage = (0, _utils.safelyRunOnBrowser)(function (_ref5) {
56
+ var window = _ref5.window;
57
+ return window.sessionStorage;
58
+ });
55
59
 
56
- if (!hasSessionStorage()) {
60
+ if (!sessionStorage) {
57
61
  return;
58
62
  }
59
63
 
60
64
  try {
61
- window.sessionStorage.setItem(KEY, JSON.stringify({
65
+ sessionStorage.setItem(KEY, JSON.stringify({
62
66
  state: getStateWithoutPage(state),
63
67
  hits: hits
64
68
  }));
@@ -88,7 +88,12 @@ var inferPayload = function inferPayload(_ref) {
88
88
  exports.inferPayload = inferPayload;
89
89
 
90
90
  var wrapInsightsClient = function wrapInsightsClient(aa, results, hits) {
91
- return function (method, payload) {
91
+ return function (method) {
92
+ for (var _len = arguments.length, payloads = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
93
+ payloads[_key - 1] = arguments[_key];
94
+ }
95
+
96
+ var payload = payloads[0];
92
97
  process.env.NODE_ENV === 'development' ? (0, _utils.warning)(false, "`insights` function has been deprecated. It is still supported in 4.x releases, but not further. It is replaced by the `insights` middleware.\n\nFor more information, visit https://www.algolia.com/doc/guides/getting-insights-and-analytics/search-analytics/click-through-and-conversions/how-to/send-click-and-conversion-events-with-instantsearch/js/") : void 0;
93
98
 
94
99
  if (!aa) {
@@ -7,6 +7,8 @@ exports.default = historyRouter;
7
7
 
8
8
  var _qs = _interopRequireDefault(require("qs"));
9
9
 
10
+ var _utils = require("../utils");
11
+
10
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
13
 
12
14
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
@@ -19,6 +21,8 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
19
21
 
20
22
  var setWindowTitle = function setWindowTitle(title) {
21
23
  if (title) {
24
+ // This function is only executed on browsers so we can disable this check.
25
+ // eslint-disable-next-line no-restricted-globals
22
26
  window.document.title = title;
23
27
  }
24
28
  };
@@ -29,11 +33,14 @@ var BrowserHistory = /*#__PURE__*/function () {
29
33
  * using web APIs (`window.location.pushState` and `onpopstate` event).
30
34
  */
31
35
  function BrowserHistory(_ref) {
36
+ var _this = this;
37
+
32
38
  var windowTitle = _ref.windowTitle,
33
39
  _ref$writeDelay = _ref.writeDelay,
34
40
  writeDelay = _ref$writeDelay === void 0 ? 400 : _ref$writeDelay,
35
41
  createURL = _ref.createURL,
36
- parseURL = _ref.parseURL;
42
+ parseURL = _ref.parseURL,
43
+ getLocation = _ref.getLocation;
37
44
 
38
45
  _classCallCheck(this, BrowserHistory);
39
46
 
@@ -45,15 +52,23 @@ var BrowserHistory = /*#__PURE__*/function () {
45
52
 
46
53
  _defineProperty(this, "parseURL", void 0);
47
54
 
55
+ _defineProperty(this, "getLocation", void 0);
56
+
48
57
  _defineProperty(this, "writeTimer", void 0);
49
58
 
59
+ _defineProperty(this, "shouldPushState", true);
60
+
50
61
  this.windowTitle = windowTitle;
51
62
  this.writeTimer = undefined;
52
63
  this.writeDelay = writeDelay;
53
64
  this._createURL = createURL;
54
65
  this.parseURL = parseURL;
55
- var title = this.windowTitle && this.windowTitle(this.read());
56
- setWindowTitle(title);
66
+ this.getLocation = getLocation;
67
+ (0, _utils.safelyRunOnBrowser)(function () {
68
+ var title = _this.windowTitle && _this.windowTitle(_this.read());
69
+
70
+ setWindowTitle(title);
71
+ });
57
72
  }
58
73
  /**
59
74
  * Reads the URL and returns a syncable UI search state.
@@ -65,7 +80,7 @@ var BrowserHistory = /*#__PURE__*/function () {
65
80
  value: function read() {
66
81
  return this.parseURL({
67
82
  qsModule: _qs.default,
68
- location: window.location
83
+ location: this.getLocation()
69
84
  });
70
85
  }
71
86
  /**
@@ -75,20 +90,30 @@ var BrowserHistory = /*#__PURE__*/function () {
75
90
  }, {
76
91
  key: "write",
77
92
  value: function write(routeState) {
78
- var _this = this;
93
+ var _this2 = this;
79
94
 
80
- var url = this.createURL(routeState);
81
- var title = this.windowTitle && this.windowTitle(routeState);
95
+ (0, _utils.safelyRunOnBrowser)(function (_ref2) {
96
+ var window = _ref2.window;
82
97
 
83
- if (this.writeTimer) {
84
- window.clearTimeout(this.writeTimer);
85
- }
98
+ var url = _this2.createURL(routeState);
99
+
100
+ var title = _this2.windowTitle && _this2.windowTitle(routeState);
101
+
102
+ if (_this2.writeTimer) {
103
+ clearTimeout(_this2.writeTimer);
104
+ }
105
+
106
+ _this2.writeTimer = setTimeout(function () {
107
+ setWindowTitle(title);
86
108
 
87
- this.writeTimer = window.setTimeout(function () {
88
- setWindowTitle(title);
89
- window.history.pushState(routeState, title || '', url);
90
- _this.writeTimer = undefined;
91
- }, this.writeDelay);
109
+ if (_this2.shouldPushState) {
110
+ window.history.pushState(routeState, title || '', url);
111
+ }
112
+
113
+ _this2.shouldPushState = true;
114
+ _this2.writeTimer = undefined;
115
+ }, _this2.writeDelay);
116
+ });
92
117
  }
93
118
  /**
94
119
  * Sets a callback on the `onpopstate` event of the history API of the current page.
@@ -98,26 +123,30 @@ var BrowserHistory = /*#__PURE__*/function () {
98
123
  }, {
99
124
  key: "onUpdate",
100
125
  value: function onUpdate(callback) {
101
- var _this2 = this;
126
+ var _this3 = this;
102
127
 
103
128
  this._onPopState = function (event) {
104
- if (_this2.writeTimer) {
105
- window.clearTimeout(_this2.writeTimer);
106
- _this2.writeTimer = undefined;
129
+ if (_this3.writeTimer) {
130
+ clearTimeout(_this3.writeTimer);
131
+ _this3.writeTimer = undefined;
107
132
  }
108
133
 
134
+ _this3.shouldPushState = false;
109
135
  var routeState = event.state; // At initial load, the state is read from the URL without update.
110
136
  // Therefore the state object is not available.
111
137
  // In this case, we fallback and read the URL.
112
138
 
113
139
  if (!routeState) {
114
- callback(_this2.read());
140
+ callback(_this3.read());
115
141
  } else {
116
142
  callback(routeState);
117
143
  }
118
144
  };
119
145
 
120
- window.addEventListener('popstate', this._onPopState);
146
+ (0, _utils.safelyRunOnBrowser)(function (_ref3) {
147
+ var window = _ref3.window;
148
+ window.addEventListener('popstate', _this3._onPopState);
149
+ });
121
150
  }
122
151
  /**
123
152
  * Creates a complete URL from a given syncable UI state.
@@ -133,7 +162,7 @@ var BrowserHistory = /*#__PURE__*/function () {
133
162
  return this._createURL({
134
163
  qsModule: _qs.default,
135
164
  routeState: routeState,
136
- location: window.location
165
+ location: this.getLocation()
137
166
  });
138
167
  }
139
168
  /**
@@ -143,12 +172,18 @@ var BrowserHistory = /*#__PURE__*/function () {
143
172
  }, {
144
173
  key: "dispose",
145
174
  value: function dispose() {
146
- if (this._onPopState) {
147
- window.removeEventListener('popstate', this._onPopState);
148
- }
175
+ var _this4 = this;
176
+
177
+ (0, _utils.safelyRunOnBrowser)(function (_ref4) {
178
+ var window = _ref4.window;
179
+
180
+ if (_this4._onPopState) {
181
+ window.removeEventListener('popstate', _this4._onPopState);
182
+ }
183
+ });
149
184
 
150
185
  if (this.writeTimer) {
151
- window.clearTimeout(this.writeTimer);
186
+ clearTimeout(this.writeTimer);
152
187
  }
153
188
 
154
189
  this.write({});
@@ -159,12 +194,12 @@ var BrowserHistory = /*#__PURE__*/function () {
159
194
  }();
160
195
 
161
196
  function historyRouter() {
162
- var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
163
- _ref2$createURL = _ref2.createURL,
164
- createURL = _ref2$createURL === void 0 ? function (_ref3) {
165
- var qsModule = _ref3.qsModule,
166
- routeState = _ref3.routeState,
167
- location = _ref3.location;
197
+ var _ref5 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
198
+ _ref5$createURL = _ref5.createURL,
199
+ createURL = _ref5$createURL === void 0 ? function (_ref6) {
200
+ var qsModule = _ref6.qsModule,
201
+ routeState = _ref6.routeState,
202
+ location = _ref6.location;
168
203
  var protocol = location.protocol,
169
204
  hostname = location.hostname,
170
205
  _location$port = location.port,
@@ -180,11 +215,11 @@ function historyRouter() {
180
215
  }
181
216
 
182
217
  return "".concat(protocol, "//").concat(hostname).concat(portWithPrefix).concat(pathname, "?").concat(queryString).concat(hash);
183
- } : _ref2$createURL,
184
- _ref2$parseURL = _ref2.parseURL,
185
- parseURL = _ref2$parseURL === void 0 ? function (_ref4) {
186
- var qsModule = _ref4.qsModule,
187
- location = _ref4.location;
218
+ } : _ref5$createURL,
219
+ _ref5$parseURL = _ref5.parseURL,
220
+ parseURL = _ref5$parseURL === void 0 ? function (_ref7) {
221
+ var qsModule = _ref7.qsModule,
222
+ location = _ref7.location;
188
223
  // `qs` by default converts arrays with more than 20 items to an object.
189
224
  // We want to avoid this because the data structure manipulated can therefore vary.
190
225
  // Setting the limit to `100` seems a good number because the engine's default is 100
@@ -198,15 +233,27 @@ function historyRouter() {
198
233
  return qsModule.parse(location.search.slice(1), {
199
234
  arrayLimit: 99
200
235
  });
201
- } : _ref2$parseURL,
202
- _ref2$writeDelay = _ref2.writeDelay,
203
- writeDelay = _ref2$writeDelay === void 0 ? 400 : _ref2$writeDelay,
204
- windowTitle = _ref2.windowTitle;
236
+ } : _ref5$parseURL,
237
+ _ref5$writeDelay = _ref5.writeDelay,
238
+ writeDelay = _ref5$writeDelay === void 0 ? 400 : _ref5$writeDelay,
239
+ windowTitle = _ref5.windowTitle,
240
+ _ref5$getLocation = _ref5.getLocation,
241
+ getLocation = _ref5$getLocation === void 0 ? function () {
242
+ return (0, _utils.safelyRunOnBrowser)(function (_ref8) {
243
+ var window = _ref8.window;
244
+ return window.location;
245
+ }, {
246
+ fallback: function fallback() {
247
+ throw new Error('You need to provide `getLocation` to the `history` router in environments where `window` does not exist.');
248
+ }
249
+ });
250
+ } : _ref5$getLocation;
205
251
 
206
252
  return new BrowserHistory({
207
253
  createURL: createURL,
208
254
  parseURL: parseURL,
209
255
  writeDelay: writeDelay,
210
- windowTitle: windowTitle
256
+ windowTitle: windowTitle,
257
+ getLocation: getLocation
211
258
  });
212
259
  }
@@ -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
  }
@@ -6,15 +6,25 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.getWidgetAttribute = getWidgetAttribute;
7
7
 
8
8
  function getWidgetAttribute(widget, initOptions) {
9
- try {
10
- // assume the type to be the correct one, but throw a nice error if it isn't the case
11
- var _getWidgetRenderState = widget.getWidgetRenderState(initOptions),
12
- widgetParams = _getWidgetRenderState.widgetParams;
9
+ var _widget$getWidgetRend;
13
10
 
14
- var attribute = 'attribute' in widgetParams ? widgetParams.attribute : widgetParams.attributes[0];
15
- if (typeof attribute !== 'string') throw new Error();
16
- return attribute;
17
- } catch (e) {
11
+ var renderState = (_widget$getWidgetRend = widget.getWidgetRenderState) === null || _widget$getWidgetRend === void 0 ? void 0 : _widget$getWidgetRend.call(widget, initOptions);
12
+ var attribute = null;
13
+
14
+ if (renderState && renderState.widgetParams) {
15
+ // casting as widgetParams is checked just before
16
+ var widgetParams = renderState.widgetParams;
17
+
18
+ if (widgetParams.attribute) {
19
+ attribute = widgetParams.attribute;
20
+ } else if (Array.isArray(widgetParams.attributes)) {
21
+ attribute = widgetParams.attributes[0];
22
+ }
23
+ }
24
+
25
+ if (typeof attribute !== 'string') {
18
26
  throw new Error("Could not find the attribute of the widget:\n\n".concat(JSON.stringify(widget), "\n\nPlease check whether the widget's getWidgetRenderState returns widgetParams.attribute correctly."));
19
27
  }
28
+
29
+ return attribute;
20
30
  }
@@ -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.31.0';
7
+ var _default = '4.33.1';
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,7 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.isMetadataEnabled = isMetadataEnabled;
7
7
  exports.createMetadataMiddleware = createMetadataMiddleware;
8
8
 
9
- function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
9
+ var _utils = require("../lib/utils");
10
10
 
11
11
  function extractPayload(widgets, instantSearchInstance, payload) {
12
12
  var parent = instantSearchInstance.mainIndex;
@@ -30,7 +30,8 @@ function extractPayload(widgets, instantSearchInstance, payload) {
30
30
  if (widget.getWidgetRenderState) {
31
31
  var renderState = widget.getWidgetRenderState(initOptions);
32
32
 
33
- if (renderState && _typeof(renderState.widgetParams) === 'object') {
33
+ if (renderState && renderState.widgetParams) {
34
+ // casting, as we just earlier checked widgetParams exists, and thus an object
34
35
  widgetParams = renderState.widgetParams;
35
36
  }
36
37
  } // since we destructure in all widgets, the parameters with defaults are set to "undefined"
@@ -52,7 +53,14 @@ function extractPayload(widgets, instantSearchInstance, payload) {
52
53
  }
53
54
 
54
55
  function isMetadataEnabled() {
55
- 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
+ });
56
64
  }
57
65
  /**
58
66
  * Exposes the metadata of mounted widgets in a custom
@@ -64,8 +72,8 @@ function isMetadataEnabled() {
64
72
 
65
73
 
66
74
  function createMetadataMiddleware() {
67
- return function (_ref) {
68
- var instantSearchInstance = _ref.instantSearchInstance;
75
+ return function (_ref2) {
76
+ var instantSearchInstance = _ref2.instantSearchInstance;
69
77
  var payload = {
70
78
  widgets: []
71
79
  };
@@ -555,7 +555,7 @@ var index = function index(widgetParams) {
555
555
  localUiState = getLocalWidgetsUiState(localWidgets, {
556
556
  searchParameters: this.getHelper().state,
557
557
  helper: this.getHelper()
558
- });
558
+ }, localUiState);
559
559
  }
560
560
  };
561
561
  };