fix(next-app-router): do not use SSR context for CSR #6534
+69
−30
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fix looks simple but it took time as it was really difficult to debug.
Basically, when navigating with category pages,
InstantSearchNext
remounts and recreates a whole new InstantSearch instance (this is a root problem we may want to deal with later by ensuring we always use a single instance).It does so too with back and forward buttons, BUT, this particular piece of code behaves differently (was really hard to find) :
instantsearch/packages/instantsearch.js/src/lib/InstantSearch.ts
Lines 698 to 710 in 7721896
defer
is really unreliable on Next.js and Remix IIRC, here when we navigate it did properly remove thenoop
, but with back and forward buttons it didn't which is why we had the initial issue where it would display empty results.The fix that was made for that worked but it caused issue #6479, as now CSR ran the hydration logic which is to add widgets during render, causing a lot of duplicate
addWidgets
in CSR.Result
Now if we're doing CSR (on the browser, no hydration), we just remove the server contexts to make sure it behaves the same way as regular
react-instantsearch
would.fixes #6479
FX-3211