Skip to content

Commit e8329ae

Browse files
authoredJun 23, 2021
fix(mainHelper): allow a mainHelper to be set before start (#4790)
1 parent 92774cb commit e8329ae

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed
 

‎src/lib/InstantSearch.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,10 @@ See ${createDocumentationLink({
425425
// This Helper is used for the queries, we don't care about its state. The
426426
// states are managed at the `index` level. We use this Helper to create
427427
// DerivedHelper scoped into the `index` widgets.
428-
const mainHelper = algoliasearchHelper(this.client, this.indexName);
428+
// In Vue InstantSearch' hydrate, a main helper gets set before start, so
429+
// we need to respect this helper as a way to keep all listeners correct.
430+
const mainHelper =
431+
this.mainHelper || algoliasearchHelper(this.client, this.indexName);
429432

430433
mainHelper.search = () => {
431434
// This solution allows us to keep the exact same API for the users but

‎src/lib/__tests__/InstantSearch-test.tsx

+22
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,28 @@ describe('start', () => {
850850
See documentation: https://www.algolia.com/doc/api-reference/widgets/instantsearch/js/"
851851
`);
852852
});
853+
854+
it('keeps a mainHelper already set on the instance (Vue SSR)', () => {
855+
const searchClient = createSearchClient();
856+
const instance = new InstantSearch({
857+
indexName: 'indexName',
858+
searchClient,
859+
});
860+
861+
const helper = algoliasearchHelper(searchClient, '');
862+
863+
// explicitly setting the mainHelper before start is used to force render to
864+
// happen before the results of the first search are done. We need to make
865+
// sure no extra helper is created, as that can cause certain things (like routing)
866+
// to be listening to the wrong helper.
867+
instance.mainHelper = helper;
868+
869+
expect(instance.mainHelper).toBe(helper);
870+
871+
instance.start();
872+
873+
expect(instance.mainHelper).toBe(helper);
874+
});
853875
});
854876

855877
describe('dispose', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.