Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(next-app-router): do not use SSR context for CSR #6534

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

aymeric-giraudet
Copy link
Member

@aymeric-giraudet aymeric-giraudet commented Jan 17, 2025

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) :

const originalScheduleSearch = this.scheduleSearch;
// We don't schedule a first search when initial results are provided
// because we already have the results to render. This skips the initial
// network request on the browser on `start`.
this.scheduleSearch = defer(noop);
// We also skip the initial network request when widgets are dynamically
// added in the first tick (that's the case in all the framework-based flavors).
// When we add a widget to `index`, it calls `scheduleSearch`. We can rely
// on our `defer` util to restore the original `scheduleSearch` value once
// widgets are added to hook back to the regular lifecycle.
defer(() => {
this.scheduleSearch = originalScheduleSearch;
})();

defer is really unreliable on Next.js and Remix IIRC, here when we navigate it did properly remove the noop, 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

@aymeric-giraudet aymeric-giraudet requested review from a team, dhayab and sarahdayan and removed request for a team January 17, 2025 10:41
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit bd74a3c:

Sandbox Source
example-react-instantsearch-getting-started Configuration
example-react-instantsearch-next-app-dir-example Configuration
example-react-instantsearch-next-routing-example Configuration
example-vue-instantsearch-getting-started Configuration

Copy link
Contributor

@Haroenv Haroenv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Duplicate Results in scopedResults with Routing Enabled in NextJS
2 participants