-
Notifications
You must be signed in to change notification settings - Fork 527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(instantsearch.js): fix user token not being set in time for the first query #6377
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit d039738:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good! probably just need added tests
packages/instantsearch.js/src/middlewares/createInsightsMiddleware.ts
Outdated
Show resolved
Hide resolved
packages/instantsearch.js/src/middlewares/createInsightsMiddleware.ts
Outdated
Show resolved
Hide resolved
@@ -287,12 +369,30 @@ export function createInsightsMiddleware< | |||
queuedUserToken, | |||
queuedAuthenticatedUserToken | |||
); | |||
} else if (anonymousUserToken) { | |||
setUserToken(anonymousUserToken, anonymousUserToken, undefined); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would like to see the setUserToken function simplified (can be separate pr) to not have three arguments
packages/instantsearch.js/src/middlewares/__tests__/createInsightsMiddleware.ts
Show resolved
Hide resolved
packages/instantsearch.js/src/lib/__tests__/InstantSearch-integration-test.ts
Show resolved
Hide resolved
packages/instantsearch.js/src/lib/__tests__/InstantSearch-integration-test.ts
Show resolved
Hide resolved
packages/instantsearch.js/src/middlewares/__tests__/createInsightsMiddleware.ts
Show resolved
Hide resolved
packages/instantsearch.js/src/middlewares/__tests__/createInsightsMiddleware.ts
Show resolved
Hide resolved
packages/instantsearch.js/src/middlewares/createInsightsMiddleware.ts
Outdated
Show resolved
Hide resolved
packages/instantsearch.js/src/middlewares/createInsightsMiddleware.ts
Outdated
Show resolved
Hide resolved
packages/instantsearch.js/src/middlewares/createInsightsMiddleware.ts
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm when the skipped test is removed
packages/instantsearch.js/src/middlewares/__tests__/createInsightsMiddleware.ts
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kudos for solving this 👏
### Context In InstantSearch, in order for user tokens to be set in time for the first query on page load, anonymous user tokens are generated and set as both the InstantSearch token and Insights token. This flow has one issue though. When `useCookies` flag needs to be changed later (such as with a cookie consent banner), a second `init` call to insights with the flag set to true does not save the existing token to a cookie. A short repro is as follows with the very latest version of InstantSearch where this behaviour was added (PR: algolia/instantsearch#6377): ```js const searchClient = algoliasearch( // ... ); window.aa('init', { appId: 'id', apiKey: 'key', }); const search = instantsearch({ indexName: 'index', searchClient, insights: true, }); search.addWidgets([ // ... ]); // This does not save the cookie atm setTimeout(() => { window.aa('init', { partial: true, useCookie: true, }); }, 5000); ```
FX-2955
Summary
Double queries happen when the state of instantsearch and insights no longer matches at the time of the first query on page load.
The
search-insights
onUserTokenChange
andonAuthenticatedUserTokenChange
callbacks are not able to set theuser-token
by the time the first search query is sent. When theuser-token
is changed by these callbacks, the state has changed from the first query. If a new query is scheduled after this(for example with dynamic widgets), the cache misses due to the changed state and second query is made.When
useCookie
is set to true, insights needs to create a anonymous token and save it as a cookie. Instantsearch would have the token updated with the callback from before, but this does not happen in time for the first query. So anonymous token generation and saving in cookies is now also handled by instantsearch.The insights queue is now being access in the middleware's
started
function to make sure any token set before instantsearch was started are part of the state by the time the first query is made.