[PR #30389] fix: Ensure chat history refreshes when switching back to conversations #32797

Closed
opened 2026-02-21 20:52:05 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/30389

State: closed
Merged: Yes


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:

  1. Query key was the same (same conversationId)
  2. refetchOnWindowFocus was set to false
  3. No staleTime was configured, so cached data was served without triggering background refetch on remount

The issue is in useShareChatList hook which fetches chat messages but didn't mark data as stale for revalidation.

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 (stale-while-revalidate pattern).

return useQuery({
  queryKey: shareQueryKeys.chatList(params),
  queryFn: () => fetchChatList(...),
  staleTime: 0,  // Always refetch in background when returning to conversation
})

Impact Analysis

Components Affected

1. chat-with-history/hooks.tsx - Main chat with conversation history
2. 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

What This Does NOT Affect

1. Initial load behavior (unchanged)
2. New conversation creation (unchanged)
3. Message sending (unchanged)
4. Other queries (conversations list, etc.)

Testing

Test 1: Switch Back to Conversation

- Open conversation A, send messages
- Switch to conversation B
- Switch back to conversation A
- Expected: Recent messages appear immediately or after brief background refresh
- Result: Pass

Test 2: No Loading Flicker

- Switch between conversations rapidly
- Expected: Cached data shown immediately, no loading spinners
- Result: Pass

Test 3: Fresh Data After Server Update

- Open conversation in two tabs
- Send message in tab 1
- Switch away and back in tab 2
- Expected: New message appears after background refetch
- Result: Pass

Unit Test Added

Added test case should always consider data stale to ensure fresh data on conversation switch (GitHub #30378) that verifies:
- First fetch returns initial data
- Remount triggers background refetch
- Data updates with fresh response

Checklist

- Reproduced the original issue
- Identified root cause (React Query cache + no staleTime)
- Analyzed impact on all components using useShareChatList
- Verified no performance regression
- Added unit test for the fix
- Tested conversation switching scenarios
- No breaking changes
**Original Pull Request:** https://github.com/langgenius/dify/pull/30389 **State:** closed **Merged:** Yes --- ## 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: 1. Query key was the same (same `conversationId`) 2. `refetchOnWindowFocus` was set to `false` 3. No `staleTime` was configured, so cached data was served without triggering background refetch on remount The issue is in `useShareChatList` hook which fetches chat messages but didn't mark data as stale for revalidation. ## 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 (stale-while-revalidate pattern). ```typescript return useQuery({ queryKey: shareQueryKeys.chatList(params), queryFn: () => fetchChatList(...), staleTime: 0, // Always refetch in background when returning to conversation }) Impact Analysis Components Affected 1. chat-with-history/hooks.tsx - Main chat with conversation history 2. 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 What This Does NOT Affect 1. Initial load behavior (unchanged) 2. New conversation creation (unchanged) 3. Message sending (unchanged) 4. Other queries (conversations list, etc.) Testing Test 1: Switch Back to Conversation - Open conversation A, send messages - Switch to conversation B - Switch back to conversation A - Expected: Recent messages appear immediately or after brief background refresh - Result: Pass Test 2: No Loading Flicker - Switch between conversations rapidly - Expected: Cached data shown immediately, no loading spinners - Result: Pass Test 3: Fresh Data After Server Update - Open conversation in two tabs - Send message in tab 1 - Switch away and back in tab 2 - Expected: New message appears after background refetch - Result: Pass Unit Test Added Added test case should always consider data stale to ensure fresh data on conversation switch (GitHub #30378) that verifies: - First fetch returns initial data - Remount triggers background refetch - Data updates with fresh response Checklist - Reproduced the original issue - Identified root cause (React Query cache + no staleTime) - Analyzed impact on all components using useShareChatList - Verified no performance regression - Added unit test for the fix - Tested conversation switching scenarios - No breaking changes
yindo added the pull-request label 2026-02-21 20:52:05 -05:00
yindo closed this issue 2026-02-21 20:52:05 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#32797