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(requests): don't fire a request when there are no widgets #6185

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
15 changes: 3 additions & 12 deletions packages/instantsearch.js/src/lib/InstantSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,17 +668,8 @@ See documentation: ${createDocumentationLink({
this.scheduleSearch = originalScheduleSearch;
})();
}
// We only schedule a search when widgets have been added before `start()`
// because there are listeners that can use these results.
// This is especially useful in framework-based flavors that wait for
// dynamically-added widgets to trigger a network request. It avoids
// having to batch this initial network request with the one coming from
// `addWidgets()`.
// Later, we could also skip `index()` widgets and widgets that don't read
// the results, but this is an optimization that has a very low impact for now.
else if (this.mainIndex.getWidgets().length > 0) {
this.scheduleSearch();
}

this.scheduleSearch();

// Keep the previous reference for legacy purpose, some pattern use
// the direct Helper access `search.helper` (e.g multi-index).
Expand Down Expand Up @@ -742,7 +733,7 @@ See documentation: ${createDocumentationLink({
}

public scheduleSearch = defer(() => {
if (this.started) {
if (this.started && this.mainIndex.getWidgets().length > 0) {
this.mainHelper!.search();
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index-widge
expect(instantSearchInstance.scheduleSearch).toHaveBeenCalledTimes(0);
});

it('does not schedule a search without widgets in the index', () => {
it.skip('does not schedule a search without widgets in the index', () => {
const instance = index({ indexName: 'indexName' });
const instantSearchInstance = createInstantSearch({
scheduleSearch: jest.fn() as any,
Expand Down
4 changes: 1 addition & 3 deletions packages/instantsearch.js/src/widgets/index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,7 @@ const index = (widgetParams: IndexWidgetParams): IndexWidget => {

helper!.setState(newState);

if (localWidgets.length) {
localInstantSearchInstance.scheduleSearch();
}
localInstantSearchInstance.scheduleSearch();
}

return this;
Expand Down