-
Notifications
You must be signed in to change notification settings - Fork 3.6k
fix(react): prevent double resume in React StrictMode #11509
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
base: main
Are you sure you want to change the base?
fix(react): prevent double resume in React StrictMode #11509
Conversation
763f0a4 to
7e31534
Compare
|
please add a patch changeset |
|
for future PRs please try to follow our PR description template |
Add a ref guard to prevent resumeStream() from being called twice when React StrictMode double-invokes effects during development. This fixes an issue where refreshing the page mid-stream would cause duplicate stream requests, leading to: - Duplicated/repeated text in the UI - Multiple concurrent stream consumers - Inconsistent message state The fix adds a hasResumedRef that tracks whether resume has already been attempted, preventing the second invocation from making another request. Fixes vercel#9610
7420ac1 to
806d9d4
Compare
Done!
Will do! |
| const hasResumedRef = useRef(false); | ||
|
|
||
| useEffect(() => { | ||
| if (resume) { | ||
| if (resume && !hasResumedRef.current) { | ||
| hasResumedRef.current = true; | ||
| chatRef.current.resumeStream(); | ||
| } | ||
| }, [resume, chatRef]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does the ref need to be reset to false? maybe i missing something here, want to make sure this works on the several resumes not just the 1st one
Background
When using
useChatwithresume: true, refreshing the page mid-stream causes duplicate stream requests because React StrictMode runs effects twice (mount → unmount → mount). The current implementation has no protection against this:This leads to:
onDatacallbacks being called twice per server writeSummary
This PR adds a ref guard to prevent
resumeStream()from being called twice when React StrictMode double-invokes effects during development.Add a
hasResumedRefthat tracks whether resume has already been attempted:Testing
Added a test case that renders the component in
<React.StrictMode>and verifies only one resume request is made despite effects running twice.Related Issues
Fixes #9610