algolia / algolia/instantsearch
Refresh does not request pages in infinite hits again
- Dominant language
- TypeScript
- Stars
- 4.1k
- Forks
- 553
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 51
Description
**Describe the bug 🐛**
When updating the `refresh` token, infinite hits will not react to the refetch.
**To Reproduce 🔍**
Steps to reproduce the behavior:
1. go to the example
2. trigger next page
3. refresh
4. see no changes in the infinite hits, even if the index would have changed
The example's index can be changed to an index you control where you make changes to make the effect clearer.
> https://codesandbox.io/s/react-instantsearch-app-dq2ro
**Expected behavior 💭**
Infinite hits reacts to refresh, either by:
1. clearing the cache completely and restarting on the current page
3. clearing the cache and the state and restarting on page 0
Likely we should go for option 2, unless there's a showPrevious.
**Additional context**
Add any other context about the problem here.
When refresh happens, we need to make sure the infinite hits internal cache is also invalidated.
On React Native, where the list component itself is stateful, we can not rely on the "key" hack, because it rerenders with an empty state when we simply clear the cache. What could be an option is:
1. clear cache
2. redo current search
3. save in cache
4. rerender
The problem is that you can't do that as a user reasonably, since you don't have access to the helper state.
A possible solution is:
In the function `refresh` in InstantSearchManager, emit an event to all widgets. Then in InfiniteHits, listen to that event, and clear the internal cache as we expect.
Another potential solution is to return a promise from the `search` which happens in refresh. This should allow people to rerender the InfiniteHits component manually.
Relevant pieces of code:
https://github.com/algolia/react-instantsearch/blob/ec9e0fbd6106d1c3e47f1dbfa6eaac3e20af6bd5/packages/react-instantsearch-core/src/core/createInstantSearchManager.js#L69-L72
Relevant issues:
https://github.com/algolia/react-instantsearch/issues/2464
Contributor guide
Research direction
Start with the refresh function in packages/react-instantsearch-core/src/core/createInstantSearchManager.js at the linked lines, then trace how InfiniteHits maintains its internal cache. Reproduce the behavior in the linked CodeSandbox after loading another page and changing the index. Done means refreshing causes InfiniteHits to invalidate its cache and display updated results according to the chosen reset behavior, including React Native considerations.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- react, react-native, typescript
- Domain
- frontend, search
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100