Open enmanuelramirez-ad opened 2 months ago
@enmanuelramirez-ad We have the same problem, it seems on the first render with data from a server component disjunctiveFacets & disjunctiveFacetsRefinements are missing in the state.
So the compare fails and the cache is resetten on back, the same is happening on dirst render.
So we have 2 workarounds, in the cache read function.
This is not the best piece of code but it works for us for now :)
import { isEqual } from 'instantsearch.js/es/lib/utils/isEqual';
const getStateWithoutPage = (state) => {
const { page, ...rest } = state || {};
return rest;
}
const isServer = typeof window === 'undefined';
// Work around for Nextjs, disjunctiveFacets & disjunctiveFacetsRefinements are missing from state, this triggers a emty hits array
const infiniteHitsCache = (() => {
let cache = null;
return {
read: ({ state }) => {
if (cache === null || isServer) {
return null;
}
// Fix first render reset hits
if (cache.first) {
cache = {
...cache,
first: false,
state: {
...cache.state,
disjunctiveFacets: state.disjunctiveFacets,
disjunctiveFacetsRefinements: state.disjunctiveFacetsRefinements
}
}
}
// Fix on back reset hits
if (state.disjunctiveFacets.length === 0) {
state = {
...state,
disjunctiveFacets: cache.state.disjunctiveFacets,
disjunctiveFacetsRefinements: cache.state.disjunctiveFacetsRefinements
}
}
return cache && isEqual(getStateWithoutPage(cache.state), getStateWithoutPage(state)) ? cache.hits : null;
},
write: ({ state, hits }) => {
cache = {
first: (state.disjunctiveFacets.length === 0),
state: state,
hits: hits
}
},
};
})();
export default infiniteHitsCache;
π Current behavior
Issue Summary
We are experiencing an issue with our application using the useInfiniteHits hook from Algolia's React InstantSearch library. When a user navigates from a Product Listing Page (PLP) to a Product Detail Page (PDP) and then uses the browser's back button to return to the PLP, the search state resets, and all product hits are cleared (the user has to click on "Load more" again).
What We Have Tried
Algolia's In-Memory Cache
Custom Cache Implementation
sessionStorage
to track product hits and search state. This approach partially works:Relevant Code Components
The following code has been simplified to keep only relevant parts of the current implementation and to provide a general overview.
InstantSearchWrapper Component
This component initializes the search client, sets up the initial UI state, and configures Algolia for search operations.
SearchQueryRenderer Component
This component dynamically generates configurations and manages the search query state.
SearchQuery Component
This component manages the search query state and utilizes
useInfiniteHits
to fetch product hits.Additional Information
Environment
^12.1.6
18.0.0
18.0.0
Any insights or recommended approaches would be greatly appreciated.
π Steps to reproduce
Steps to Reproduce:
Live reproduction
none
π Expected behavior
Expected Behavior:
The search state and product hits should be persisted when navigating back from the PDP to the PLP, ensuring a seamless user experience without re-fetching data.
Package version
react-instantsearch-core: ^7.1.0, algoliasearch: ^4.20.0, instantsearch.js: ^4.63.0
Operating system
macOS 14.4.1
Browser
Google Chrome 128.0.6613.113
Code of Conduct