fix(chat): stop blanking a conversation (and wiping its cache) on a failed thread fetch

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 <noreply@anthropic.com>
This commit is contained in:
2026-07-07 13:47:58 +05:30
parent 12be747609
commit 507489ec55
+13 -3
View File
@@ -788,7 +788,7 @@
<body>
<script src="/icons.js?v=4"></script>
<script src="https://cdn.jsdelivr.net/npm/@twemoji/api@15.1.0/dist/twemoji.min.js" crossorigin="anonymous"></script>
<script>window.__BUILD='2026-07-07-batch44';console.log('%cBiz Connect','color:#1F3B73;font-weight:bold','build '+window.__BUILD);
<script>window.__BUILD='2026-07-07-batch45';console.log('%cBiz Connect','color:#1F3B73;font-weight:bold','build '+window.__BUILD);
// Render modern (Twemoji) emojis in place of the OS's flat ones. No-op if the CDN didn't load
// (emojis stay as plain Unicode). (#5)
function twemojify(el){ try{ if(el && window.twemoji) window.twemoji.parse(el, { folder:'svg', ext:'.svg' }); }catch(_){} }</script>
@@ -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,