Skip to content

Commit

Permalink
fix(mainHelper): allow a mainHelper to be set before start (#4790)
Browse files Browse the repository at this point in the history
  • Loading branch information
Haroenv committed Jul 1, 2021
1 parent 8c0fccb commit a83f706
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/lib/InstantSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,10 @@ See ${createDocumentationLink({
// This Helper is used for the queries, we don't care about its state. The
// states are managed at the `index` level. We use this Helper to create
// DerivedHelper scoped into the `index` widgets.
const mainHelper = algoliasearchHelper(this.client, this.indexName);
// In Vue InstantSearch' hydrate, a main helper gets set before start, so
// we need to respect this helper as a way to keep all listeners correct.
const mainHelper =
this.mainHelper || algoliasearchHelper(this.client, this.indexName);

mainHelper.search = () => {
// This solution allows us to keep the exact same API for the users but
Expand Down
22 changes: 22 additions & 0 deletions src/lib/__tests__/InstantSearch-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,28 @@ describe('start', () => {
See documentation: https://www.algolia.com/doc/api-reference/widgets/instantsearch/js/"
`);
});

it('keeps a mainHelper already set on the instance (Vue SSR)', () => {
const searchClient = createSearchClient();
const instance = new InstantSearch({
indexName: 'indexName',
searchClient,
});

const helper = algoliasearchHelper(searchClient, '');

// explicitly setting the mainHelper before start is used to force render to
// happen before the results of the first search are done. We need to make
// sure no extra helper is created, as that can cause certain things (like routing)
// to be listening to the wrong helper.
instance.mainHelper = helper;

expect(instance.mainHelper).toBe(helper);

instance.start();

expect(instance.mainHelper).toBe(helper);
});
});

describe('dispose', () => {
Expand Down

0 comments on commit a83f706

Please sign in to comment.