react-instantsearch 7.13.5 → 7.13.6

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.
@@ -7,7 +7,7 @@
7
7
 
8
8
  var React__default = 'default' in React ? React['default'] : React;
9
9
 
10
- var version = '7.13.5';
10
+ var version = '7.13.6';
11
11
 
12
12
  // Copyright Joyent, Inc. and other Node contributors.
13
13
  //
@@ -11059,6 +11059,22 @@
11059
11059
  return getCookie(ANONYMOUS_TOKEN_COOKIE_KEY);
11060
11060
  }
11061
11061
 
11062
+ /**
11063
+ * Create UUID according to
11064
+ * https://www.ietf.org/rfc/rfc4122.txt.
11065
+ *
11066
+ * @returns Generated UUID.
11067
+ */
11068
+ function createUUID() {
11069
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
11070
+ /* eslint-disable no-bitwise */
11071
+ var r = Math.random() * 16 | 0;
11072
+ var v = c === 'x' ? r : r & 0x3 | 0x8;
11073
+ /* eslint-enable */
11074
+ return v.toString(16);
11075
+ });
11076
+ }
11077
+
11062
11078
  function _typeof$l(obj) {
11063
11079
  "@babel/helpers - typeof";
11064
11080
 
@@ -11189,6 +11205,7 @@
11189
11205
  $$internal = _props$$$internal === void 0 ? false : _props$$$internal,
11190
11206
  _props$$$automatic = props.$$automatic,
11191
11207
  $$automatic = _props$$$automatic === void 0 ? false : _props$$$automatic;
11208
+ var currentTokenType;
11192
11209
  var potentialInsightsClient = _insightsClient;
11193
11210
  if (!_insightsClient && _insightsClient !== null) {
11194
11211
  safelyRunOnBrowser(function (_ref) {
@@ -11232,6 +11249,7 @@
11232
11249
  _getAppIdAndApiKey2 = _slicedToArray$7(_getAppIdAndApiKey, 2),
11233
11250
  appId = _getAppIdAndApiKey2[0],
11234
11251
  apiKey = _getAppIdAndApiKey2[1];
11252
+ var queuedInitParams = undefined;
11235
11253
  var queuedUserToken = undefined;
11236
11254
  var queuedAuthenticatedUserToken = undefined;
11237
11255
  var userTokenBeforeInit = undefined;
@@ -11248,7 +11266,7 @@
11248
11266
  // At this point, even though `search-insights` is not loaded yet,
11249
11267
  // we still want to read the token from the queue.
11250
11268
  // Otherwise, the first search call will be fired without the token.
11251
- var _map = ['setUserToken', 'setAuthenticatedUserToken'].map(function (key) {
11269
+ var _map = ['setUserToken', 'setAuthenticatedUserToken', 'init'].map(function (key) {
11252
11270
  var _ref3 = find$1(queue.slice().reverse(), function (_ref5) {
11253
11271
  var _ref6 = _slicedToArray$7(_ref5, 1),
11254
11272
  method = _ref6[0];
@@ -11258,9 +11276,10 @@
11258
11276
  value = _ref4[1];
11259
11277
  return value;
11260
11278
  });
11261
- var _map2 = _slicedToArray$7(_map, 2);
11279
+ var _map2 = _slicedToArray$7(_map, 3);
11262
11280
  queuedUserToken = _map2[0];
11263
11281
  queuedAuthenticatedUserToken = _map2[1];
11282
+ queuedInitParams = _map2[2];
11264
11283
  }
11265
11284
 
11266
11285
  // If user called `aa('setUserToken')` or `aa('setAuthenticatedUserToken')`
@@ -11311,6 +11330,23 @@
11311
11330
  started: function started() {
11312
11331
  insightsClient('addAlgoliaAgent', 'insights-middleware');
11313
11332
  helper = instantSearchInstance.mainHelper;
11333
+ var queueAtStart = insightsClient.queue;
11334
+ if (Array.isArray(queueAtStart)) {
11335
+ var _map3 = ['setUserToken', 'setAuthenticatedUserToken', 'init'].map(function (key) {
11336
+ var _ref7 = find$1(queueAtStart.slice().reverse(), function (_ref9) {
11337
+ var _ref10 = _slicedToArray$7(_ref9, 1),
11338
+ method = _ref10[0];
11339
+ return method === key;
11340
+ }) || [],
11341
+ _ref8 = _slicedToArray$7(_ref7, 2),
11342
+ value = _ref8[1];
11343
+ return value;
11344
+ });
11345
+ var _map4 = _slicedToArray$7(_map3, 3);
11346
+ queuedUserToken = _map4[0];
11347
+ queuedAuthenticatedUserToken = _map4[1];
11348
+ queuedInitParams = _map4[2];
11349
+ }
11314
11350
  initialParameters = {
11315
11351
  userToken: helper.state.userToken,
11316
11352
  clickAnalytics: helper.state.clickAnalytics
@@ -11326,8 +11362,9 @@
11326
11362
  if (!$$internal) {
11327
11363
  instantSearchInstance.scheduleSearch();
11328
11364
  }
11329
- var setUserTokenToSearch = function setUserTokenToSearch(userToken) {
11330
- var immediate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
11365
+ var setUserTokenToSearch = function setUserTokenToSearch(userToken, tokenType) {
11366
+ var immediate = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
11367
+ var unsetAuthenticatedUserToken = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
11331
11368
  var normalizedUserToken = normalizeUserToken(userToken);
11332
11369
  if (!normalizedUserToken) {
11333
11370
  return;
@@ -11340,6 +11377,13 @@
11340
11377
  if (existingToken && existingToken !== userToken) {
11341
11378
  instantSearchInstance.scheduleSearch();
11342
11379
  }
11380
+ currentTokenType = tokenType;
11381
+ }
11382
+
11383
+ // the authenticated user token cannot be overridden by a user or anonymous token
11384
+ // for instant search query requests
11385
+ if (currentTokenType && currentTokenType === 'authenticated' && tokenType === 'default' && !unsetAuthenticatedUserToken) {
11386
+ return;
11343
11387
  }
11344
11388
 
11345
11389
  // Delay the token application to the next render cycle
@@ -11349,14 +11393,8 @@
11349
11393
  applyToken();
11350
11394
  }
11351
11395
  };
11352
- var anonymousUserToken = getInsightsAnonymousUserTokenInternal();
11353
- if (anonymousUserToken) {
11354
- // When `aa('init', { ... })` is called, it creates an anonymous user token in cookie.
11355
- // We can set it as userToken.
11356
- setUserTokenToSearch(anonymousUserToken, true);
11357
- }
11358
11396
  function setUserToken(token, userToken, authenticatedUserToken) {
11359
- setUserTokenToSearch(token, true);
11397
+ setUserTokenToSearch(token, authenticatedUserToken ? 'authenticated' : 'default', true);
11360
11398
  if (userToken) {
11361
11399
  insightsClient('setUserToken', userToken);
11362
11400
  }
@@ -11364,19 +11402,66 @@
11364
11402
  insightsClient('setAuthenticatedUserToken', authenticatedUserToken);
11365
11403
  }
11366
11404
  }
11405
+ var anonymousUserToken = undefined;
11406
+ var anonymousTokenFromInsights = getInsightsAnonymousUserTokenInternal();
11407
+ if (anonymousTokenFromInsights) {
11408
+ // When `aa('init', { ... })` is called, it creates an anonymous user token in cookie.
11409
+ // We can set it as userToken on instantsearch and insights. If it's not set as an insights
11410
+ // userToken before a sendEvent, insights automatically generates a new anonymous token,
11411
+ // causing a state change and an unnecessary query on instantsearch.
11412
+ anonymousUserToken = anonymousTokenFromInsights;
11413
+ } else {
11414
+ var token = "anonymous-".concat(createUUID());
11415
+ anonymousUserToken = token;
11416
+ }
11417
+ var authenticatedUserTokenFromInit;
11418
+ var userTokenFromInit;
11419
+
11420
+ // With SSR, the token could be be set on the state. We make sure
11421
+ // that insights is in sync with that token since, there is no
11422
+ // insights lib on the server.
11423
+ var tokenFromSearchParameters = initialParameters.userToken;
11424
+
11425
+ // When the first query is sent, the token is possibly not yet be set by
11426
+ // the insights onChange callbacks (if insights isn't yet loaded).
11427
+ // It is explicitly being set here so that the first query has the
11428
+ // initial tokens set and ensure a second query isn't automatically
11429
+ // made when the onChange callback actually changes the state.
11430
+ if (insightsInitParams) {
11431
+ if (insightsInitParams.authenticatedUserToken) {
11432
+ authenticatedUserTokenFromInit = insightsInitParams.authenticatedUserToken;
11433
+ } else if (insightsInitParams.userToken) {
11434
+ userTokenFromInit = insightsInitParams.userToken;
11435
+ }
11436
+ }
11367
11437
 
11368
11438
  // We consider the `userToken` or `authenticatedUserToken` before an
11369
- // `init` call of higher importance than one from the queue.
11439
+ // `init` call of higher importance than one from the queue and ones set
11440
+ // from the init props to be higher than that.
11441
+ var tokenFromInit = authenticatedUserTokenFromInit || userTokenFromInit;
11370
11442
  var tokenBeforeInit = authenticatedUserTokenBeforeInit || userTokenBeforeInit;
11371
- var queuedToken = queuedAuthenticatedUserToken || queuedUserToken;
11372
- if (tokenBeforeInit) {
11443
+ var tokenFromQueue = queuedAuthenticatedUserToken || queuedUserToken;
11444
+ if (tokenFromInit) {
11445
+ setUserToken(tokenFromInit, userTokenFromInit, authenticatedUserTokenFromInit);
11446
+ } else if (tokenFromSearchParameters) {
11447
+ setUserToken(tokenFromSearchParameters, tokenFromSearchParameters, undefined);
11448
+ } else if (tokenBeforeInit) {
11373
11449
  setUserToken(tokenBeforeInit, userTokenBeforeInit, authenticatedUserTokenBeforeInit);
11374
- } else if (queuedToken) {
11375
- setUserToken(queuedToken, queuedUserToken, queuedAuthenticatedUserToken);
11450
+ } else if (tokenFromQueue) {
11451
+ setUserToken(tokenFromQueue, queuedUserToken, queuedAuthenticatedUserToken);
11452
+ } else if (anonymousUserToken) {
11453
+ var _queuedInitParams;
11454
+ setUserToken(anonymousUserToken, anonymousUserToken, undefined);
11455
+ if (insightsInitParams !== null && insightsInitParams !== void 0 && insightsInitParams.useCookie || (_queuedInitParams = queuedInitParams) !== null && _queuedInitParams !== void 0 && _queuedInitParams.useCookie) {
11456
+ var _queuedInitParams2;
11457
+ saveTokenAsCookie(anonymousUserToken, (insightsInitParams === null || insightsInitParams === void 0 ? void 0 : insightsInitParams.cookieDuration) || ((_queuedInitParams2 = queuedInitParams) === null || _queuedInitParams2 === void 0 ? void 0 : _queuedInitParams2.cookieDuration));
11458
+ }
11376
11459
  }
11377
11460
 
11378
11461
  // This updates userToken which is set explicitly by `aa('setUserToken', userToken)`
11379
- insightsClient('onUserTokenChange', setUserTokenToSearch, {
11462
+ insightsClient('onUserTokenChange', function (token) {
11463
+ return setUserTokenToSearch(token, 'default', true);
11464
+ }, {
11380
11465
  immediate: true
11381
11466
  });
11382
11467
 
@@ -11385,10 +11470,10 @@
11385
11470
  // If we're unsetting the `authenticatedUserToken`, we revert to the `userToken`
11386
11471
  if (!authenticatedUserToken) {
11387
11472
  insightsClient('getUserToken', null, function (_, userToken) {
11388
- setUserTokenToSearch(userToken);
11473
+ setUserTokenToSearch(userToken, 'default', true, true);
11389
11474
  });
11390
11475
  }
11391
- setUserTokenToSearch(authenticatedUserToken);
11476
+ setUserTokenToSearch(authenticatedUserToken, 'authenticated', true);
11392
11477
  }, {
11393
11478
  immediate: true
11394
11479
  });
@@ -11434,6 +11519,13 @@
11434
11519
  };
11435
11520
  };
11436
11521
  }
11522
+ function saveTokenAsCookie(token, cookieDuration) {
11523
+ var MONTH = 30 * 24 * 60 * 60 * 1000;
11524
+ var d = new Date();
11525
+ d.setTime(d.getTime() + (cookieDuration || MONTH * 6));
11526
+ var expires = "expires=".concat(d.toUTCString());
11527
+ document.cookie = "_ALGOLIA=".concat(token, ";").concat(expires, ";path=/");
11528
+ }
11437
11529
 
11438
11530
  /**
11439
11531
  * Determines if a given insights `client` supports the optional call to `init`
@@ -13092,7 +13184,7 @@
13092
13184
  };
13093
13185
  }
13094
13186
 
13095
- var version$3 = '4.75.2';
13187
+ var version$3 = '4.75.3';
13096
13188
 
13097
13189
  function _typeof$q(obj) {
13098
13190
  "@babel/helpers - typeof";