fix: Ensure chat history refreshes when switching back to conversations#30389
Conversation
## Problem When switching back to a previously viewed conversation, the chat history would show stale data (missing recent messages) until switching away and back again. Users had to double-switch to see their latest messages. ## Root Cause React Query cached the chat list data and served stale cache when returning to a conversation because: 1. Query key was the same (same conversationId) 2. refetchOnWindowFocus was set to false 3. No staleTime was configured, defaulting to 0 but cache was being reused without triggering background refetch on remount ## Solution Added staleTime: 0 to useShareChatList hook to ensure data is always considered stale. This triggers a background refetch when returning to a conversation while still showing cached data immediately (no loading flicker). ## Impact Analysis Components affected: - chat-with-history/hooks.tsx - Main chat with conversation history - embedded-chatbot/hooks.tsx - Embedded chatbot component Both components benefit from this fix with no breaking changes. ## Why staleTime: 0 is safe 1. Returns cached data immediately (no loading state/flicker) 2. Background refetch is transparent to user 3. Network calls are deduplicated by React Query 4. No performance regression - same API behavior, fresher data ## Testing Added test case verifying that switching back to a conversation triggers a background refetch and updates with fresh data. Fixes langgenius#30378
Summary of ChangesHello @devbyteai, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a critical bug in the chat functionality where conversation history would not refresh properly when users navigated back to a previously viewed chat. By strategically configuring React Query's caching mechanism to always consider chat list data as stale, the change guarantees that the most current messages are displayed without introducing loading flickers, significantly improving the user experience for chat interactions. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This is an excellent pull request that effectively resolves the issue of stale chat history. The root cause analysis is accurate, and the solution of setting staleTime: 0 on the useShareChatList query is a simple and idiomatic way to implement the desired 'stale-while-revalidate' behavior in React Query. This ensures a smooth user experience by showing cached data immediately while fetching fresh data in the background. The new unit test is particularly well-written; it accurately simulates the component lifecycle of switching conversations (unmount and remount) and thoroughly validates that the data is correctly updated. The changes are clean, well-documented, and demonstrate a solid understanding of the problem and the library being used. No issues were found.
Summary
Fixes #30378 - Chat conversation history displays incorrectly when switching back to a previously viewed conversation.
Problem
When switching back to a previously viewed conversation, the chat history would show stale data (missing recent messages) until switching away and back again. Users had to double-switch to see their latest messages.
Root Cause Analysis
React Query cached the chat list data and served stale cache when returning to a conversation because:
conversationId)refetchOnWindowFocuswas set tofalsestaleTimewas configured, so cached data was served without triggering background refetch on remountThe issue is in
useShareChatListhook which fetches chat messages but didn't mark data as stale for revalidation.Solution
Added
staleTime: 0touseShareChatListhook to ensure data is always considered stale. This triggers a background refetch when returning to a conversation while still showing cached data immediately (stale-while-revalidate pattern).