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

SearchParams Sync Issue During First Effect Execution with window.history.replaceState() #72322

Open
yongholeeme opened this issue Nov 5, 2024 · 2 comments
Labels
bug Issue was opened via the bug report template. Navigation Related to Next.js linking (e.g., <Link>) and navigation.

Comments

@yongholeeme
Copy link

yongholeeme commented Nov 5, 2024

Link to the code that reproduces this issue

https://github.com/yongholeeme/reproduction-bug-history-api-in-effect-nextjs/blob/main/Bug.jsx

To Reproduce

I created and deployed reproduction: https://reproduction-bug-history-api-in-effect-nextjs.vercel.app

  1. When query foo is over 5, call window.history.replaceState() in effect
  2. When searchParams is changed, call console.log() to watch searchParams
  3. When clicked the button, query foo is updated by random number (0 ~ 9)
  1. When connect https://reproduction-bug-history-api-in-effect-nextjs.vercel.app?foo=100, foo's value has to be changed bar according to 1. When query foo is over 5, call window.history.replaceState() in effect
  2. It seems to be changed to ?foo=bar, but console.log() don't be executed in useEffect has searchParams dependency.
  3. Even after calling router.refresh(), ?foo=100 query is back.
Screen.Recording.2024-11-05.at.5.53.22.PM.mov

Current vs. Expected behavior

Wherever window.history.replaceState() is called, it's be synchronized with Router.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.0.0: Tue Sep 24 23:37:13 PDT 2024; root:xnu-11215.1.12~1/RELEASE_ARM64_T8112
  Available memory (MB): 24576
  Available CPU cores: 8
Binaries:
  Node: 20.13.1
  npm: 10.5.2
  Yarn: N/A
  pnpm: 9.1.1
Relevant Packages:
  next: 15.0.2 // Latest available version is detected (15.0.2).
  eslint-config-next: N/A
  react: 19.0.0-rc-02c0e824-20241028
  react-dom: 19.0.0-rc-02c0e824-20241028
  typescript: N/A
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Navigation

Which stage(s) are affected? (Select all that apply)

next dev (local), next start (local), Vercel (Deployed)

Additional context

No response

@yongholeeme yongholeeme added the bug Issue was opened via the bug report template. label Nov 5, 2024
@github-actions github-actions bot added the Navigation Related to Next.js linking (e.g., <Link>) and navigation. label Nov 5, 2024
@Sahil4883
Copy link

Going onto the network tab I noticed there's a fetch call every 5 click and there's no fetch call in between those 5 click

@yongholeeme
Copy link
Author

yongholeeme commented Nov 7, 2024

@Sahil4883, those fetch calls are triggered by router.refresh(). It's called whenever foo query is updated with a value greater than 5. https://github.com/yongholeeme/reproduction-bug-history-api-in-effect-nextjs/blob/main/Bug.jsx#L15-L20

I wrote this code to show that window.history.replaceState() in first effect execution doesn't synchronize with next router.

Screen.Recording.2024-11-07.at.1.16.04.PM.mov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Navigation Related to Next.js linking (e.g., <Link>) and navigation.
Projects
None yet
Development

No branches or pull requests

2 participants