fix: Prevent duplicate chat message rendering caused by React StrictM…#5951
Open
yuyol wants to merge 3 commits intoFlowiseAI:mainfrom
Open
fix: Prevent duplicate chat message rendering caused by React StrictM…#5951yuyol wants to merge 3 commits intoFlowiseAI:mainfrom
yuyol wants to merge 3 commits intoFlowiseAI:mainfrom
Conversation
…ode double-invocation
Author
|
/gemini review |
Contributor
|
Warning Gemini encountered an error creating the summary. You can try again by commenting |
Contributor
There was a problem hiding this comment.
Code Review
The pull request addresses a bug where duplicate chat messages were rendered due to React StrictMode's double invocation of useEffect in development. The fix involves deduplicating messages based on their IDs before appending them to the message list. This review focuses on the correctness and maintainability of the implemented solution.
…hat messages without an ID are always included.
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.
Problem
In the
ChatMessagecomponent, thegetChatmessageApi(andgetAllExecutionsApi) was being called twice on mount. While the API was always invoked twice, the chat messages were sometimes rendered in duplicate — appearing as two copies of the entire conversation history.Root Cause (Observed behavior)
The application uses
React.StrictMode(packages/ui/src/index.jsx), which in React 18 development mode intentionally double-invokesuseEffectcallbacks (mount → cleanup → re-mount) to help surface impure side effects.This caused the initialization
useEffect(dependent on[open, chatflowid]) to callgetChatmessageApi.request(chatflowid)twice, resulting in two network requests.The data-handling
useEffect(dependent on[getChatmessageApi.data]) used a simple append pattern:When both API responses arrived after the StrictMode cleanup had already reset the messages state,
loadedMessageswas appended twice, causing duplicate rendering. When timing was different (first response arrived before cleanup), only one copy survived — explaining the intermittent nature of the bug.Fix
Changed the
setMessagescalls in both thegetChatmessageApi.dataandgetAllExecutionsApi.datauseEffecthandlers from a simple append to a deduplicated append:This makes the operation idempotent — no matter how many times the API data triggers the effect, messages with the same
idwill not be duplicated. Messages without anid(e.g., the initial welcome message, in-progress user messages) are always preserved.Changes
packages/ui/src/views/chatmessage/ChatMessage.jsxgetChatmessageApi.datauseEffect: replaced[...prevMessages, ...loadedMessages]with deduplicated merge byidgetAllExecutionsApi.datauseEffect: same deduplication fixImpact
id(welcome message, streaming messages in progress) are unaffected by the filter logicHow to test
getChatmessageApimay be requested twice