From 507489ec551c3398108dad865ae36a4ca1e27a6a Mon Sep 17 00:00:00 2001 From: sravan Date: Tue, 7 Jul 2026 13:47:58 +0530 Subject: [PATCH] fix(chat): stop blanking a conversation (and wiping its cache) on a failed thread fetch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit THE disappearing-messages root cause. openConvo did: let msgs=[]; try{ msgs=fetch() }catch{}; THREAD = Array.isArray(msgs)?msgs:[]. On a flaky desktop network a failed fetch left msgs=[] (still an array) → THREAD=[] AND THREAD_CACHE.set(ckey,[]) — so reopening a chat blanked it AND overwrote the cache, making messages vanish and stay gone even though they persist server-side (confirmed: 982 DMs, zero dangling ids). Now msgs stays null on a failed/non-OK fetch; we only replace THREAD/cache on a real array response, and otherwise keep the cached render instead of blanking. Co-Authored-By: Claude Opus 4.8 --- server/public/home.html | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/server/public/home.html b/server/public/home.html index 5210cdd..28fc5bf 100644 --- a/server/public/home.html +++ b/server/public/home.html @@ -788,7 +788,7 @@ - @@ -1795,9 +1795,19 @@ async function openConvo(kind,id){ if(!selected||selected.kind!==kind||selected.id!==id) return; wireMentions(); const url=kind==='group'?('/api/messages/thread?group='+encodeURIComponent(id)):('/api/messages/thread?with='+encodeURIComponent(id)); - let msgs=[]; try{ msgs=await fetch(url).then(r=>r.json()); }catch(_){} + // msgs stays null on a failed/non-OK fetch (distinct from a genuinely empty [] thread), so a flaky + // desktop network can't be mistaken for "no messages". + let msgs=null; try{ const r=await fetch(url); if(r.ok) msgs=await r.json(); }catch(_){} if(!selected||selected.kind!==kind||selected.id!==id) return; // switched away while loading - THREAD=Array.isArray(msgs)?msgs:[]; + if(!Array.isArray(msgs)){ + // Fetch failed / errored — DO NOT blank the chat or overwrite the cache (that wiped messages that + // are still safely on the server → the "messages disappear after reopen" bug). Keep the cached + // render; only show empty if we truly have nothing cached to fall back to. + if(!THREAD_CACHE.has(ckey)){ THREAD=[]; renderThread(); } + const inp0=document.getElementById('msgInput'); if(inp0) inp0.focus(); + return; + } + THREAD=msgs; THREAD_CACHE.set(ckey, THREAD.slice()); renderThread(); // #3: if there were unread messages, drop a "New messages" divider, scroll to the first unread,