Skip to content
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(userToken): prevent search waterfall #5512

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import type { JSDOM } from 'jsdom';
import type { PlainSearchParameters } from 'algoliasearch-helper';
import { fireEvent } from '@testing-library/dom';
import { createInstantSearch } from '../../../test/createInstantSearch';
import { castToJestMock } from '@instantsearch/testutils';
import { connectSearchBox } from '../../connectors';

declare const jsdom: JSDOM;

Expand Down Expand Up @@ -43,6 +45,7 @@ describe('insights', () => {
started = true,
insights = false,
} = {}) => {
castToJestMock(searchClient.search).mockClear();
const { analytics, insightsClient } = createInsights();
const indexName = 'my-index';
const instantSearchInstance = instantsearch({
Expand Down Expand Up @@ -107,6 +110,8 @@ describe('insights', () => {
(window as any).aa = undefined;

document.body.innerHTML = '';

document.cookie = '_ALGOLIA=;';
});

describe('usage', () => {
Expand Down Expand Up @@ -621,6 +626,76 @@ See documentation: https://www.algolia.com/doc/guides/building-search-ui/going-f
).toEqual('def');
});

it('searches once per unique userToken', async () => {
const { insightsClient, instantSearchInstance } = createTestEnvironment();

instantSearchInstance.addWidgets([connectSearchBox(() => ({}))({})]);

await wait(0);
expect(instantSearchInstance.client.search).toHaveBeenCalledTimes(1);

insightsClient('setUserToken', 'abc');
instantSearchInstance.use(
createInsightsMiddleware({
insightsClient,
})
);

await wait(0);
expect(instantSearchInstance.client.search).toHaveBeenCalledTimes(2);

insightsClient('setUserToken', 'abc');

await wait(0);
expect(instantSearchInstance.client.search).toHaveBeenCalledTimes(2);
});

it("doesn't search when userToken is falsy", async () => {
const { insightsClient, instantSearchInstance } = createTestEnvironment();

instantSearchInstance.addWidgets([connectSearchBox(() => ({}))({})]);

await wait(0);
expect(instantSearchInstance.client.search).toHaveBeenCalledTimes(1);
expect(instantSearchInstance.client.search).toHaveBeenLastCalledWith([
{
indexName: 'my-index',
params: {
facets: [],
query: '',
tagFilters: '',
},
},
]);

insightsClient('setUserToken', 0);
instantSearchInstance.use(
createInsightsMiddleware({
insightsClient,
insightsInitParams: { useCookie: false },
})
);

await wait(0);
expect(instantSearchInstance.client.search).toHaveBeenCalledTimes(2);
expect(instantSearchInstance.client.search).toHaveBeenLastCalledWith([
{
indexName: 'my-index',
params: {
clickAnalytics: true,
facets: [],
query: '',
tagFilters: '',
},
},
]);

insightsClient('setUserToken', undefined);

await wait(0);
expect(instantSearchInstance.client.search).toHaveBeenCalledTimes(2);
});

describe('umd', () => {
it('applies userToken from queue if exists', () => {
const {
Expand Down Expand Up @@ -816,8 +891,6 @@ See documentation: https://www.algolia.com/doc/guides/building-search-ui/going-f
await wait(100);
// url should not get cleared
expect(document.location.href).toEqual(url);

document.cookie = '';
});

test('does not throw error when document or cookie are undefined', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,21 @@ export function createInsightsMiddleware<
}

const setUserTokenToSearch = (userToken?: string) => {
if (!userToken) {
return;
}
Haroenv marked this conversation as resolved.
Show resolved Hide resolved

const existingToken = (helper.state as PlainSearchParameters)
.userToken;

helper.overrideStateWithoutTriggeringChangeEvent({
...helper.state,
userToken,
});

instantSearchInstance.scheduleSearch();
if (existingToken && existingToken !== userToken) {
instantSearchInstance.scheduleSearch();
}
};

const anonymousUserToken = getInsightsAnonymousUserTokenInternal();
Expand All @@ -204,8 +213,10 @@ export function createInsightsMiddleware<
// We consider the `userToken` coming from a `init` call to have a higher
// importance than the one coming from the queue.
if (userTokenBeforeInit) {
setUserTokenToSearch(userTokenBeforeInit);
insightsClient('setUserToken', userTokenBeforeInit);
} else if (queuedUserToken) {
setUserTokenToSearch(queuedUserToken);
insightsClient('setUserToken', queuedUserToken);
}

Expand Down