Skip to content

fix: debounce chat session persistence to prevent excessive localStor… - #534

Merged
AnuranjanJain merged 3 commits into
AnuranjanJain:mainfrom
priyanshu5ingh:fix/chat-debounce-persistence
Aug 2, 2026
Merged

fix: debounce chat session persistence to prevent excessive localStor…#534
AnuranjanJain merged 3 commits into
AnuranjanJain:mainfrom
priyanshu5ingh:fix/chat-debounce-persistence

Conversation

@priyanshu5ingh

Copy link
Copy Markdown
Contributor

🔀 Pull Request

📌 Issue Reference

Closes #431


📝 Summary

Problem: The chat page persisted the entire conversation to localStorage on every messages state change via useEffect at ChatbotPage.tsx:128-132. During AI streaming responses, setMessages was called on each received chunk (dozens of times per response), each triggering a full synchronous localStorage.setItem that blocked the main thread and degraded UI responsiveness.

Solution: Debounced the persistence effect using a generic useDebounce hook with a 1500ms delay, so localStorage is 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

File Change
src/hooks/useDebounce.ts New — Generic useDebounce<T>(value, delay) hook that returns a debounced value, updating only after delay ms of inactivity
src/pages/ChatbotPage.tsx Added useDebounce import; 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 unmount
src/test/hooks/useDebounce.test.ts New — 6 unit tests covering initial value, delay gating, timer reset on rapid changes, numeric types, and zero-delay edge case

📸 Screenshots

N/A — performance improvement, no visual change.


✅ Checklist

  • My code follows the project's coding conventions
  • I have tested all impacted features
  • I have updated or added necessary documentation

🏅 Open Source Program Participation

Program Name: GSSoC 2026


💬 Additional Notes

  • Debounce delay (1500ms): Long enough to batch all stream chunks into a single write, short enough to feel responsive after sending a message.
  • Unmount safety: Refs (latestMessagesRef, latestDocRef, latestSessionIdRef) are updated on every render so the useEffect cleanup (which has empty deps) can access the absolute latest values — not stale closure values.
  • Testing: All 148 tests pass (13 suites, +6 new hook tests, 0 regressions).
  • Streaming behavior: Before this fix, a response with 50 chunks would trigger 50+ synchronous localStorage writes on the main thread. After this fix, only 1 write fires 1.5s after the stream completes.

@vercel

vercel Bot commented Jul 12, 2026

Copy link
Copy Markdown

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.

@AnuranjanJain

Copy link
Copy Markdown
Owner

@priyanshu5ingh can you look for the merge conflicts

@AnuranjanJain AnuranjanJain added GSSoC'26 level:intermediate Requires some experience type:refactor Code restructuring gssoc:approved Approved for GSSOC contribution labels Jul 14, 2026
@github-actions

Copy link
Copy Markdown

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.

@github-actions github-actions Bot added the stale label Jul 22, 2026
@priyanshu5ingh

Copy link
Copy Markdown
Contributor Author

@AnuranjanJain i have committed the conflicts

@github-actions github-actions Bot removed the stale label Jul 23, 2026
@github-actions

Copy link
Copy Markdown

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.

@github-actions github-actions Bot added the stale label Jul 31, 2026
@vercel

vercel Bot commented Aug 2, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
legal-ease Ready Ready Preview Aug 2, 2026 1:23pm

@AnuranjanJain
AnuranjanJain merged commit 382c256 into AnuranjanJain:main Aug 2, 2026
9 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gssoc:approved Approved for GSSOC contribution GSSoC'26 level:intermediate Requires some experience stale type:refactor Code restructuring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The chat page persists the entire conversation to localStorage on every messages state change.

2 participants