algolia / instantsearch

⚑️ Libraries for building performant and instant search and recommend experiences with Algolia. Compatible with JavaScript, TypeScript, React and Vue.

Home Page:https://www.algolia.com/doc/guides/building-search-ui/what-is-instantsearch/js/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

When root indexName is absent, `scopedResults` are empty

marcelgerber opened this issue Β· comments

πŸ› Current behavior

If I create a <InstantSearch indexName={undefined}> element (as enabled by #5590 a year ago), with several <Index> components inside it, and then want to access useInstantSearch()'s scopedResults element, I find that it will always be (essentially) empty.

πŸ” Steps to reproduce

See the repro link below; click the checkbox at the very top and see how the available indexIds change.

Live reproduction

https://codesandbox.io/p/sandbox/algolia-no-root-index-scopedresults-w2jfvl?file=%2Fsrc%2FApp.tsx

πŸ’­ Expected behavior

I should find entries in scopedResults for all sub-indexes, as is the case if a root index name is given.

Package version

Algolia for JavaScript (4.23.3); Browser (lite); instantsearch.js (4.66.1); react (18.1.0); react-instantsearch (7.7.1); react-instantsearch-core (7.7.1); JS Helper (3.17.0)

Operating system

No response

Browser

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct

So, I could trace this down to these lines just now:

// Results can be `null` when the first search is stalled.
// In this case, we skip the update.
// See: https://github.com/algolia/instantsearch/blob/20996c7a159988c58e00ff24d2d2dc98af8b980f/src/widgets/index/index.ts#L652-L657
if (results !== null) {
setSearchResults({
results,
scopedResults: searchIndex.getScopedResults(),
});
}
}

In the above case, results is always null (makes sense, I guess), but searchIndex.getScopedResults() is not. But because of this if-statement, it is never updated.

That's a good point, although I'm not sure how to change this condition without then also making people deal with null for the initial render (which people usually don't have to care about, and otherwise destructures badly). An option could be a way to go, but isn't that nice. Do you have a solution in mind?

Hm, I don't know about the internals of InstantSearch and when results are arriving, but can we detect that there is no main index present?
In that case, results will always be null (and has always been), and it makes sense to update scopedResults directly.

Yes, that's a good point, so in that case the condition could be:

if (results !== null) {
  setSearchResults({
    results,
    scopedResults: searchIndex.getScopedResults(),
  });
} else if (search.mainIndex.getIndexName() === undefined) {
  setSearchResults({
    results: getIndexSearchResults(searchIndex),
    scopedResults: searchIndex.getScopedResults(),
  });
}

If you're interested in seeing this through, we'd be happy to accept a pull request with this feature and new tests :)

commented

Hi, we just released react-instantsearch@7.7.3 and react-instantsearch-core@7.7.3 which include a fix for this issue.

Thank you @dhayab, works perfectly and flawlessly!