instantsearch.js 4.59.0 → 4.60.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.
@@ -457,6 +457,7 @@ var InstantSearch = /*#__PURE__*/function (_EventEmitter) {
457
457
  uiState: this._initialUiState
458
458
  });
459
459
  if (this._initialResults) {
460
+ (0, _utils.hydrateSearchClient)(this.client, this._initialResults);
460
461
  var originalScheduleSearch = this.scheduleSearch;
461
462
  // We don't schedule a first search when initial results are provided
462
463
  // because we already have the results to render. This skips the initial
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.hydrateSearchClient = hydrateSearchClient;
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
+ // @ts-nocheck (types to be fixed during actual implementation)
14
+
15
+ function hydrateSearchClient(client, results) {
16
+ if (!results) {
17
+ return;
18
+ }
19
+
20
+ // Disable cache hydration on:
21
+ // - Algoliasearch API Client < v4 with cache disabled
22
+ // - Third party clients (detected by the `addAlgoliaAgent` function missing)
23
+
24
+ if ((!client.transporter || client._cacheHydrated) && (!client._useCache || typeof client.addAlgoliaAgent !== 'function')) {
25
+ return;
26
+ }
27
+
28
+ // Algoliasearch API Client >= v4
29
+ // To hydrate the client we need to populate the cache with the data from
30
+ // the server (done in `hydrateSearchClientWithMultiIndexRequest` or
31
+ // `hydrateSearchClientWithSingleIndexRequest`). But since there is no way
32
+ // for us to compute the key the same way as `algoliasearch-client` we need
33
+ // to populate it on a custom key and override the `search` method to
34
+ // search on it first.
35
+ if (client.transporter && !client._cacheHydrated) {
36
+ client._cacheHydrated = true;
37
+ var baseMethod = client.search;
38
+ client.search = function (requests) {
39
+ for (var _len = arguments.length, methodArgs = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
40
+ methodArgs[_key - 1] = arguments[_key];
41
+ }
42
+ var requestsWithSerializedParams = requests.map(function (request) {
43
+ return _objectSpread(_objectSpread({}, request), {}, {
44
+ params: serializeQueryParameters(request.params)
45
+ });
46
+ });
47
+ return client.transporter.responsesCache.get({
48
+ method: 'search',
49
+ args: [requestsWithSerializedParams].concat(methodArgs)
50
+ }, function () {
51
+ return baseMethod.apply(void 0, [requests].concat(methodArgs));
52
+ });
53
+ };
54
+
55
+ // Populate the cache with the data from the server
56
+ client.transporter.responsesCache.set({
57
+ method: 'search',
58
+ args: [Object.keys(results).reduce(function (acc, key) {
59
+ return acc.concat(results[key].results.map(function (request) {
60
+ return {
61
+ indexName: request.index,
62
+ params: request.params
63
+ };
64
+ }));
65
+ }, [])]
66
+ }, {
67
+ results: Object.keys(results).reduce(function (acc, key) {
68
+ return acc.concat(results[key].results);
69
+ }, [])
70
+ });
71
+ }
72
+
73
+ // Algoliasearch API Client < v4
74
+ // Prior to client v4 we didn't have a proper API to hydrate the client
75
+ // cache from the outside. The following code populates the cache with
76
+ // a single-index result. You can find more information about the
77
+ // computation of the key inside the client (see link below).
78
+ // https://github.com/algolia/algoliasearch-client-javascript/blob/c27e89ff92b2a854ae6f40dc524bffe0f0cbc169/src/AlgoliaSearchCore.js#L232-L240
79
+ if (!client.transporter) {
80
+ var cacheKey = "/1/indexes/*/queries_body_".concat(JSON.stringify({
81
+ requests: Object.keys(results).reduce(function (acc, key) {
82
+ return acc.concat(results[key].rawResults.map(function (request) {
83
+ return {
84
+ indexName: request.index,
85
+ params: request.params
86
+ };
87
+ }));
88
+ }, [])
89
+ }));
90
+ client.cache = _objectSpread(_objectSpread({}, client.cache), {}, _defineProperty({}, cacheKey, JSON.stringify({
91
+ results: Object.keys(results).reduce(function (acc, key) {
92
+ return acc.concat(results[key].rawResults);
93
+ }, [])
94
+ })));
95
+ }
96
+ }
97
+
98
+ // This function is copied from the algoliasearch v4 API Client. If modified,
99
+ // consider updating it also in `serializeQueryParameters` from `@algolia/transporter`.
100
+ function serializeQueryParameters(parameters) {
101
+ var isObjectOrArray = function isObjectOrArray(value) {
102
+ return Object.prototype.toString.call(value) === '[object Object]' || Object.prototype.toString.call(value) === '[object Array]';
103
+ };
104
+ var encode = function encode(format) {
105
+ for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
106
+ args[_key2 - 1] = arguments[_key2];
107
+ }
108
+ var i = 0;
109
+ return format.replace(/%s/g, function () {
110
+ return encodeURIComponent(args[i++]);
111
+ });
112
+ };
113
+ return Object.keys(parameters).map(function (key) {
114
+ return encode('%s=%s', key, isObjectOrArray(parameters[key]) ? JSON.stringify(parameters[key]) : parameters[key]);
115
+ }).join('&');
116
+ }
@@ -322,6 +322,17 @@ Object.keys(_hitsQueryId).forEach(function (key) {
322
322
  }
323
323
  });
324
324
  });
325
+ var _hydrateSearchClient = require("./hydrateSearchClient");
326
+ Object.keys(_hydrateSearchClient).forEach(function (key) {
327
+ if (key === "default" || key === "__esModule") return;
328
+ if (key in exports && exports[key] === _hydrateSearchClient[key]) return;
329
+ Object.defineProperty(exports, key, {
330
+ enumerable: true,
331
+ get: function get() {
332
+ return _hydrateSearchClient[key];
333
+ }
334
+ });
335
+ });
325
336
  var _isDomElement = require("./isDomElement");
326
337
  Object.keys(_isDomElement).forEach(function (key) {
327
338
  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.59.0';
7
+ var _default = '4.60.0';
8
8
  exports.default = _default;
@@ -1,4 +1,4 @@
1
- /*! InstantSearch.js 4.59.0 | © Algolia, Inc. and contributors; MIT License | https://github.com/algolia/instantsearch */
1
+ /*! InstantSearch.js 4.60.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) :
@@ -1411,6 +1411,111 @@
1411
1411
  });
1412
1412
  }
1413
1413
 
1414
+ // @ts-nocheck (types to be fixed during actual implementation)
1415
+
1416
+ function hydrateSearchClient(client, results) {
1417
+ if (!results) {
1418
+ return;
1419
+ }
1420
+
1421
+ // Disable cache hydration on:
1422
+ // - Algoliasearch API Client < v4 with cache disabled
1423
+ // - Third party clients (detected by the `addAlgoliaAgent` function missing)
1424
+
1425
+ if ((!client.transporter || client._cacheHydrated) && (!client._useCache || typeof client.addAlgoliaAgent !== 'function')) {
1426
+ return;
1427
+ }
1428
+
1429
+ // Algoliasearch API Client >= v4
1430
+ // To hydrate the client we need to populate the cache with the data from
1431
+ // the server (done in `hydrateSearchClientWithMultiIndexRequest` or
1432
+ // `hydrateSearchClientWithSingleIndexRequest`). But since there is no way
1433
+ // for us to compute the key the same way as `algoliasearch-client` we need
1434
+ // to populate it on a custom key and override the `search` method to
1435
+ // search on it first.
1436
+ if (client.transporter && !client._cacheHydrated) {
1437
+ client._cacheHydrated = true;
1438
+ var baseMethod = client.search;
1439
+ client.search = function (requests) {
1440
+ for (var _len = arguments.length, methodArgs = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
1441
+ methodArgs[_key - 1] = arguments[_key];
1442
+ }
1443
+ var requestsWithSerializedParams = requests.map(function (request) {
1444
+ return _objectSpread2(_objectSpread2({}, request), {}, {
1445
+ params: serializeQueryParameters(request.params)
1446
+ });
1447
+ });
1448
+ return client.transporter.responsesCache.get({
1449
+ method: 'search',
1450
+ args: [requestsWithSerializedParams].concat(methodArgs)
1451
+ }, function () {
1452
+ return baseMethod.apply(void 0, [requests].concat(methodArgs));
1453
+ });
1454
+ };
1455
+
1456
+ // Populate the cache with the data from the server
1457
+ client.transporter.responsesCache.set({
1458
+ method: 'search',
1459
+ args: [Object.keys(results).reduce(function (acc, key) {
1460
+ return acc.concat(results[key].results.map(function (request) {
1461
+ return {
1462
+ indexName: request.index,
1463
+ params: request.params
1464
+ };
1465
+ }));
1466
+ }, [])]
1467
+ }, {
1468
+ results: Object.keys(results).reduce(function (acc, key) {
1469
+ return acc.concat(results[key].results);
1470
+ }, [])
1471
+ });
1472
+ }
1473
+
1474
+ // Algoliasearch API Client < v4
1475
+ // Prior to client v4 we didn't have a proper API to hydrate the client
1476
+ // cache from the outside. The following code populates the cache with
1477
+ // a single-index result. You can find more information about the
1478
+ // computation of the key inside the client (see link below).
1479
+ // https://github.com/algolia/algoliasearch-client-javascript/blob/c27e89ff92b2a854ae6f40dc524bffe0f0cbc169/src/AlgoliaSearchCore.js#L232-L240
1480
+ if (!client.transporter) {
1481
+ var cacheKey = "/1/indexes/*/queries_body_".concat(JSON.stringify({
1482
+ requests: Object.keys(results).reduce(function (acc, key) {
1483
+ return acc.concat(results[key].rawResults.map(function (request) {
1484
+ return {
1485
+ indexName: request.index,
1486
+ params: request.params
1487
+ };
1488
+ }));
1489
+ }, [])
1490
+ }));
1491
+ client.cache = _objectSpread2(_objectSpread2({}, client.cache), {}, _defineProperty({}, cacheKey, JSON.stringify({
1492
+ results: Object.keys(results).reduce(function (acc, key) {
1493
+ return acc.concat(results[key].rawResults);
1494
+ }, [])
1495
+ })));
1496
+ }
1497
+ }
1498
+
1499
+ // This function is copied from the algoliasearch v4 API Client. If modified,
1500
+ // consider updating it also in `serializeQueryParameters` from `@algolia/transporter`.
1501
+ function serializeQueryParameters(parameters) {
1502
+ var isObjectOrArray = function isObjectOrArray(value) {
1503
+ return Object.prototype.toString.call(value) === '[object Object]' || Object.prototype.toString.call(value) === '[object Array]';
1504
+ };
1505
+ var encode = function encode(format) {
1506
+ for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
1507
+ args[_key2 - 1] = arguments[_key2];
1508
+ }
1509
+ var i = 0;
1510
+ return format.replace(/%s/g, function () {
1511
+ return encodeURIComponent(args[i++]);
1512
+ });
1513
+ };
1514
+ return Object.keys(parameters).map(function (key) {
1515
+ return encode('%s=%s', key, isObjectOrArray(parameters[key]) ? JSON.stringify(parameters[key]) : parameters[key]);
1516
+ }).join('&');
1517
+ }
1518
+
1414
1519
  function isPrimitive(obj) {
1415
1520
  return obj !== Object(obj);
1416
1521
  }
@@ -14640,7 +14745,7 @@
14640
14745
  };
14641
14746
  }
14642
14747
 
14643
- var version$1 = '4.59.0';
14748
+ var version$1 = '4.60.0';
14644
14749
 
14645
14750
  var withUsage$r = createDocumentationMessageGenerator({
14646
14751
  name: 'instantsearch'
@@ -15068,6 +15173,7 @@
15068
15173
  uiState: this._initialUiState
15069
15174
  });
15070
15175
  if (this._initialResults) {
15176
+ hydrateSearchClient(this.client, this._initialResults);
15071
15177
  var originalScheduleSearch = this.scheduleSearch;
15072
15178
  // We don't schedule a first search when initial results are provided
15073
15179
  // because we already have the results to render. This skips the initial