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.
@@ -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, noop, warning, setIndexHelperState, isIndexWidget } from "./utils/index.js";
24
+ import { createDocumentationMessageGenerator, createDocumentationLink, defer, 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'
@@ -449,6 +449,7 @@ var InstantSearch = /*#__PURE__*/function (_EventEmitter) {
449
449
  uiState: this._initialUiState
450
450
  });
451
451
  if (this._initialResults) {
452
+ hydrateSearchClient(this.client, this._initialResults);
452
453
  var originalScheduleSearch = this.scheduleSearch;
453
454
  // We don't schedule a first search when initial results are provided
454
455
  // because we already have the results to render. This skips the initial
@@ -0,0 +1,2 @@
1
+ import type { InitialResults, SearchClient } from '../../types';
2
+ export declare function hydrateSearchClient(client: SearchClient, results?: InitialResults): void;
@@ -0,0 +1,110 @@
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
+ // @ts-nocheck (types to be fixed during actual implementation)
8
+
9
+ export function hydrateSearchClient(client, results) {
10
+ if (!results) {
11
+ return;
12
+ }
13
+
14
+ // Disable cache hydration on:
15
+ // - Algoliasearch API Client < v4 with cache disabled
16
+ // - Third party clients (detected by the `addAlgoliaAgent` function missing)
17
+
18
+ if ((!client.transporter || client._cacheHydrated) && (!client._useCache || typeof client.addAlgoliaAgent !== 'function')) {
19
+ return;
20
+ }
21
+
22
+ // Algoliasearch API Client >= v4
23
+ // To hydrate the client we need to populate the cache with the data from
24
+ // the server (done in `hydrateSearchClientWithMultiIndexRequest` or
25
+ // `hydrateSearchClientWithSingleIndexRequest`). But since there is no way
26
+ // for us to compute the key the same way as `algoliasearch-client` we need
27
+ // to populate it on a custom key and override the `search` method to
28
+ // search on it first.
29
+ if (client.transporter && !client._cacheHydrated) {
30
+ client._cacheHydrated = true;
31
+ var baseMethod = client.search;
32
+ client.search = function (requests) {
33
+ for (var _len = arguments.length, methodArgs = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
34
+ methodArgs[_key - 1] = arguments[_key];
35
+ }
36
+ var requestsWithSerializedParams = requests.map(function (request) {
37
+ return _objectSpread(_objectSpread({}, request), {}, {
38
+ params: serializeQueryParameters(request.params)
39
+ });
40
+ });
41
+ return client.transporter.responsesCache.get({
42
+ method: 'search',
43
+ args: [requestsWithSerializedParams].concat(methodArgs)
44
+ }, function () {
45
+ return baseMethod.apply(void 0, [requests].concat(methodArgs));
46
+ });
47
+ };
48
+
49
+ // Populate the cache with the data from the server
50
+ client.transporter.responsesCache.set({
51
+ method: 'search',
52
+ args: [Object.keys(results).reduce(function (acc, key) {
53
+ return acc.concat(results[key].results.map(function (request) {
54
+ return {
55
+ indexName: request.index,
56
+ params: request.params
57
+ };
58
+ }));
59
+ }, [])]
60
+ }, {
61
+ results: Object.keys(results).reduce(function (acc, key) {
62
+ return acc.concat(results[key].results);
63
+ }, [])
64
+ });
65
+ }
66
+
67
+ // Algoliasearch API Client < v4
68
+ // Prior to client v4 we didn't have a proper API to hydrate the client
69
+ // cache from the outside. The following code populates the cache with
70
+ // a single-index result. You can find more information about the
71
+ // computation of the key inside the client (see link below).
72
+ // https://github.com/algolia/algoliasearch-client-javascript/blob/c27e89ff92b2a854ae6f40dc524bffe0f0cbc169/src/AlgoliaSearchCore.js#L232-L240
73
+ if (!client.transporter) {
74
+ var cacheKey = "/1/indexes/*/queries_body_".concat(JSON.stringify({
75
+ requests: Object.keys(results).reduce(function (acc, key) {
76
+ return acc.concat(results[key].rawResults.map(function (request) {
77
+ return {
78
+ indexName: request.index,
79
+ params: request.params
80
+ };
81
+ }));
82
+ }, [])
83
+ }));
84
+ client.cache = _objectSpread(_objectSpread({}, client.cache), {}, _defineProperty({}, cacheKey, JSON.stringify({
85
+ results: Object.keys(results).reduce(function (acc, key) {
86
+ return acc.concat(results[key].rawResults);
87
+ }, [])
88
+ })));
89
+ }
90
+ }
91
+
92
+ // This function is copied from the algoliasearch v4 API Client. If modified,
93
+ // consider updating it also in `serializeQueryParameters` from `@algolia/transporter`.
94
+ function serializeQueryParameters(parameters) {
95
+ var isObjectOrArray = function isObjectOrArray(value) {
96
+ return Object.prototype.toString.call(value) === '[object Object]' || Object.prototype.toString.call(value) === '[object Array]';
97
+ };
98
+ var encode = function encode(format) {
99
+ for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
100
+ args[_key2 - 1] = arguments[_key2];
101
+ }
102
+ var i = 0;
103
+ return format.replace(/%s/g, function () {
104
+ return encodeURIComponent(args[i++]);
105
+ });
106
+ };
107
+ return Object.keys(parameters).map(function (key) {
108
+ return encode('%s=%s', key, isObjectOrArray(parameters[key]) ? JSON.stringify(parameters[key]) : parameters[key]);
109
+ }).join('&');
110
+ }
@@ -27,6 +27,7 @@ export * from './getRefinements';
27
27
  export * from './getWidgetAttribute';
28
28
  export * from './hits-absolute-position';
29
29
  export * from './hits-query-id';
30
+ export * from './hydrateSearchClient';
30
31
  export * from './isDomElement';
31
32
  export * from './isEqual';
32
33
  export * from './isFacetRefined';
@@ -27,6 +27,7 @@ export * from "./getRefinements.js";
27
27
  export * from "./getWidgetAttribute.js";
28
28
  export * from "./hits-absolute-position.js";
29
29
  export * from "./hits-query-id.js";
30
+ export * from "./hydrateSearchClient.js";
30
31
  export * from "./isDomElement.js";
31
32
  export * from "./isEqual.js";
32
33
  export * from "./isFacetRefined.js";
@@ -1,2 +1,2 @@
1
- declare const _default: "4.59.0";
1
+ declare const _default: "4.60.0";
2
2
  export default _default;
package/es/lib/version.js CHANGED
@@ -1 +1 @@
1
- export default '4.59.0';
1
+ export default '4.60.0';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "instantsearch.js",
3
- "version": "4.59.0",
3
+ "version": "4.60.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",
@@ -55,9 +55,9 @@
55
55
  "version": "./scripts/version/update-version.js"
56
56
  },
57
57
  "devDependencies": {
58
- "@instantsearch/mocks": "1.28.0",
59
- "@instantsearch/tests": "1.28.0",
60
- "@instantsearch/testutils": "1.17.0",
58
+ "@instantsearch/mocks": "1.29.0",
59
+ "@instantsearch/tests": "1.29.0",
60
+ "@instantsearch/testutils": "1.18.0",
61
61
  "@storybook/html": "5.3.9",
62
62
  "@types/scriptjs": "0.0.2",
63
63
  "algoliasearch": "4.14.3",
@@ -65,5 +65,5 @@
65
65
  "scriptjs": "2.5.9",
66
66
  "webpack": "4.41.5"
67
67
  },
68
- "gitHead": "8fb537f2301a5e133e9e5df20c7c208cae16b3aa"
68
+ "gitHead": "cacb63f88ce9f215e8f65af2b497e615a58b296e"
69
69
  }