fix: debounce chat session persistence to prevent excessive localStor… - #534
Merged
AnuranjanJain merged 3 commits intoAug 2, 2026
Merged
Conversation
…age writes during streaming
|
Someone is attempting to deploy a commit to the WizardKing's projects Team on Vercel. A member of the Team first needs to authorize it. |
Owner
|
@priyanshu5ingh can you look for the merge conflicts |
|
This PR has been inactive for 7 days. Please update the PR to keep it open. It will be closed in 3 days if there is no further activity. |
Contributor
Author
|
@AnuranjanJain i have committed the conflicts |
|
This PR has been inactive for 7 days. Please update the PR to keep it open. It will be closed in 3 days if there is no further activity. |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
AnuranjanJain
self-requested a review
August 2, 2026 13:23
AnuranjanJain
approved these changes
Aug 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
🔀 Pull Request
📌 Issue Reference
Closes #431
📝 Summary
Problem: The chat page persisted the entire conversation to
localStorageon everymessagesstate change viauseEffectatChatbotPage.tsx:128-132. During AI streaming responses,setMessageswas called on each received chunk (dozens of times per response), each triggering a full synchronouslocalStorage.setItemthat blocked the main thread and degraded UI responsiveness.Solution: Debounced the persistence effect using a generic
useDebouncehook with a 1500ms delay, solocalStorageis only written after a quiet period. Added unmount-flush logic via refs to ensure no data is lost when navigating away mid-stream.Major Changes
src/hooks/useDebounce.tsuseDebounce<T>(value, delay)hook that returns a debounced value, updating only afterdelayms of inactivitysrc/pages/ChatbotPage.tsxuseDebounceimport; replaced[messages, uploadedDoc]persist effect with debounced variant; added refs (latestMessagesRef,latestDocRef,latestSessionIdRef) for unmount flush; added cleanup effect that persists latest messages synchronously on unmountsrc/test/hooks/useDebounce.test.ts📸 Screenshots
N/A — performance improvement, no visual change.
✅ Checklist
🏅 Open Source Program Participation
Program Name: GSSoC 2026
💬 Additional Notes
latestMessagesRef,latestDocRef,latestSessionIdRef) are updated on every render so theuseEffectcleanup (which has empty deps) can access the absolute latest values — not stale closure values.localStoragewrites on the main thread. After this fix, only 1 write fires 1.5s after the stream completes.