fix(web): clear stale conversation_id from localStorage on 404#34945
fix(web): clear stale conversation_id from localStorage on 404#34945HamzaSwitch wants to merge 1 commit intolanggenius:mainfrom
Conversation
When a Web App chatbot conversation is deleted server-side, the client would enter an infinite retry loop of 404s on GET /api/messages. The stale conversation_id in localStorage was never cleared, and TanStack Query kept retrying (3x per cycle) on every window focus. - useShareChatList now disables retries for 404 responses - fetchChatList passes silent: true so the 404 does not spam toasts - Both embedded-chatbot and chat-with-history hooks now watch the error state and clear the stale conversation_id via removeConversationIdInfo(appId) when the 404 is observed, which disables the query and falls back to a fresh conversation Fixes langgenius#34731
|
Hi @HamzaSwitch! I'm Dosu and I’m helping the dify team. This is a clean, well-scoped fix. The three-pronged approach — suppress toast with A few observations:
One thing worth double-checking: in the Overall this looks good to me — minimal surface area, well-tested, and directly addresses the infinite-loop symptom without masking errors or widening scope. 👍 To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Fixes #34731.
When a conversation has been deleted on the server but the Web App chatbot still has its
conversation_idin localStorage, the page enters an infinite loop: it refetchesGET /api/messages?conversation_id=..., gets404, shows a toast, and retries again every couple of seconds. The "Reset conversation" button only clears the visual state, not the localStorage entry, so the only real workaround is for the user to manually clear site data.The reporter and @dosu already traced the root cause pretty well. Summarizing:
useShareChatListinweb/service/use-share.tsdoesn't configureretry, so TanStack Query uses its default (3 retries) and also refetches on window focus.fetchChatListdoesn't passsilent: true, so every failed attempt fires a toast viaafterResponseErrorCodeinweb/service/fetch.ts.embedded-chatbot/hooks.tsxandchat-with-history/hooks.tsxdestructure onlydataandisLoadingfromuseShareChatList— theerrorstate is ignored, so nothing clears the staleconversation_idfrom localStorage when the 404 comes back.The fix
Three small changes, all frontend:
web/service/share.ts—fetchChatListnow passessilent: trueso a 404 response doesn't spam toasts. The 404 is a legitimate "conversation no longer exists" signal that the hook layer handles; it shouldn't look like a server error to the user.web/service/use-share.ts—useShareChatListnow disables retries for 404s:Other HTTP failures still get the default 3 retries.
web/app/components/base/chat/embedded-chatbot/hooks.tsxandweb/app/components/base/chat/chat-with-history/hooks.tsx— both now readerrorfromuseShareChatListand run auseEffectthat callsremoveConversationIdInfo(appId)when the error is a 404Response. Clearing the stale entry causescurrentConversationIdto fall back to'', which in turn disables the query (sinceisEnabledchecks!!params.conversationId), and the chatbot starts a fresh conversation.chat-with-history/hooks.tsxdidn't previously have aremoveConversationIdInfohelper, so I added one matching the shape of the existingembedded-chatbotimplementation.Why this shape instead of alternatives
Testing
Manually reproduced on a self-hosted stack (
docker compose -p dify up -d) with a Chatflow app exposed as a Web App:conversationIdInfois populated inApplication → Local Storage → conversationIdInfo.Before the fix: Network tab shows
GET /api/messages?...returning 404 every 2–3 seconds, toast error pops up repeatedly, page never recovers. Clicking "Reset conversation" does nothing useful.After the fix: Exactly one
GET /api/messages?...404 in the network tab, no toast,conversationIdInfois cleared from localStorage on the same tick, the chatbot falls back to the new-conversation state automatically.Also re-tested the happy path (send messages in a valid conversation, switch conversations, refresh) to confirm I didn't break the normal chat-list loading flow.