Skip to content

Commit 1b9b5f4

Browse files
samoussfrancoischalifour
authored andcommitted
fix(index): prevent render without results (#3932)
* fix(index): prevent render without results * refactor(InstantSearch): search stalled false by default * docs: comments wording Co-Authored-By: François Chalifour <francoischalifour@users.noreply.github.com>
1 parent 3aafbad commit 1b9b5f4

File tree

4 files changed

+44
-27
lines changed

4 files changed

+44
-27
lines changed

src/lib/InstantSearch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ See ${createDocumentationLink({
127127

128128
this._stalledSearchDelay = stalledSearchDelay;
129129
this._searchStalledTimer = null;
130-
this._isSearchStalled = true;
130+
this._isSearchStalled = false;
131131
this._searchParameters = {
132132
...searchParameters,
133133
index: indexName,
@@ -370,7 +370,7 @@ See ${createDocumentationLink({
370370
});
371371

372372
scheduleStalledRender() {
373-
if (!this._isSearchStalled && !this._searchStalledTimer) {
373+
if (!this._searchStalledTimer) {
374374
this._searchStalledTimer = setTimeout(() => {
375375
this._isSearchStalled = true;
376376
this.scheduleRender();

src/lib/__tests__/InstantSearch-test.js

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -969,25 +969,6 @@ describe('scheduleStalledRender', () => {
969969
})
970970
);
971971
});
972-
973-
// https://github.com/algolia/instantsearch.js/pull/2623
974-
it('does not trigger a re-`render` without results', () => {
975-
const { searchClient } = createControlledSearchClient();
976-
const search = new InstantSearch({
977-
indexName: 'index_name',
978-
searchClient,
979-
});
980-
981-
search.start();
982-
983-
search.addWidget({
984-
render: () => {},
985-
});
986-
987-
jest.runOnlyPendingTimers();
988-
989-
return runAllMicroTasks();
990-
});
991972
});
992973

993974
describe('createURL', () => {

src/widgets/index/__tests__/index-test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,40 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index/js/"
745745
});
746746
});
747747
});
748+
749+
// https://github.com/algolia/instantsearch.js/pull/2623
750+
it('does not call `render` without `lastResults`', () => {
751+
const instance = index({ indexName: 'index_name' });
752+
const instantSearchInstance = createInstantSearch();
753+
754+
const widgets = [createSearchBox(), createPagination()];
755+
756+
instance.addWidgets(widgets);
757+
758+
widgets.forEach(widget => {
759+
expect(widget.render).toHaveBeenCalledTimes(0);
760+
});
761+
762+
instance.init(
763+
createInitOptions({
764+
instantSearchInstance,
765+
})
766+
);
767+
768+
widgets.forEach(widget => {
769+
expect(widget.render).toHaveBeenCalledTimes(0);
770+
});
771+
772+
instance.render(
773+
createRenderOptions({
774+
instantSearchInstance,
775+
})
776+
);
777+
778+
widgets.forEach(widget => {
779+
expect(widget.render).toHaveBeenCalledTimes(0);
780+
});
781+
});
748782
});
749783

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

src/widgets/index/index.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,16 +238,18 @@ const index = (props: IndexProps): Index => {
238238
render({ instantSearchInstance }) {
239239
localWidgets.forEach(widget => {
240240
// At this point, all the variables used below are set. Both `helper`
241-
// and `derivedHelper` has been created at the `init` step. The attribute
242-
// `lastResults` is set before the event `result` is emitted. At this stage,
243-
// the event has emitted hence the value is already set.
241+
// and `derivedHelper` have been created at the `init` step. The attribute
242+
// `lastResults` might be `null` though. It's possible that a stalled render
243+
// happens before the result e.g with a dynamically added index the request might
244+
// be delayed. The render is triggered for the complete tree but some parts do
245+
// not have results yet.
244246

245-
if (widget.render) {
247+
if (widget.render && derivedHelper!.lastResults) {
246248
widget.render({
247249
helper: helper!,
248250
instantSearchInstance,
249-
results: derivedHelper!.lastResults!,
250-
state: derivedHelper!.lastResults!._state,
251+
results: derivedHelper!.lastResults,
252+
state: derivedHelper!.lastResults._state,
251253
templatesConfig: instantSearchInstance.templatesConfig,
252254
createURL: instantSearchInstance._createAbsoluteURL,
253255
searchMetadata: {

0 commit comments

Comments
 (0)