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(react-instantsearch): index added twice with Next.js app router #6106

Merged
merged 1 commit into from
Mar 27, 2024

Conversation

aymeric-giraudet
Copy link
Member

@aymeric-giraudet aymeric-giraudet commented Mar 27, 2024

Summary

This should fix what @MikeIbberson talks about here : #6073 (comment)

The problem was index widgets would get added twice client-side in a Next.js App Router application.
This is due to how useLayoutEffect behaves on either React canary or an RSC context (not sure), before what we had was :

  • index gets added on render because shouldSsr is true and the parent index doesn't contain it yet, making shouldAddWidgetEarly be true
  • useLayoutEffect is called, shouldAddWidgetEarly is true so we don't add it again
  • useLayoutEffect dispose function would be called and set the cleanupTimer
  • shouldAddWidgetEarly is now false because parentIndex contains the child index
  • useLayoutEffect called again, with shouldAddWidgetEarly being false this time, but it still skips adding the widget again because the cleanup timer is defined

In React 19 canary / Next.js, the cleanup function does not get called (maybe no strict mode or a different one), which means the last point fails.

Result

We were just using the wrong dependency in useEffect, we should never add the widget in an effect if shouldSsr is true, and that way the effect will run less often.
Only an index widget can have shouldSsr be true client-side

@aymeric-giraudet aymeric-giraudet requested review from a team, dhayab and sarahdayan and removed request for a team March 27, 2024 09:15
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 9e6daa9:

Sandbox Source
react-instantsearch-app Configuration
example-react-instantsearch-default-theme Configuration
example-react-instantsearch-next-app-dir-example Configuration
example-vue-instantsearch-default-theme Configuration

Copy link
Member

@dhayab dhayab left a comment

Choose a reason for hiding this comment

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

While we're waiting until the API we use are stable to implement a suite of unit tests, this package could benefit from a simple e2e implementation in a future PR to check this scenario (similar to what we have for react-instantsearch-router-nextjs).

@aymeric-giraudet aymeric-giraudet merged commit 1778b31 into master Mar 27, 2024
12 checks passed
@aymeric-giraudet aymeric-giraudet deleted the fix/index-added-twice-nextjs branch March 27, 2024 10:33
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.

None yet

2 participants