Skip to content

Commit c5cbf93

Browse files
fix(web): a newer pending favicon shows over an older stored one
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent e870f30 commit c5cbf93

2 files changed

Lines changed: 49 additions & 11 deletions

File tree

‎apps/web/src/browserFaviconStore.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ vi.mock("~/state/session", () => ({
2020
import { useComposerDraftStore, type DraftThreadState } from "./composerDraftStore";
2121
import {
2222
lookupFavicon,
23+
lookupFaviconForThread,
2324
recordFaviconForProject,
2425
recordFaviconForThread,
2526
resetBrowserFaviconsForTests,
@@ -127,6 +128,27 @@ describe("browser favicon store", () => {
127128
expect(Object.values(useBrowserFaviconStore.getState().pendingByThreadKey)[0]).toBeDefined();
128129
});
129130

131+
it("shows a newer pending capture over the stored favicon for the same origin", () => {
132+
const NEWER = "data:image/png;base64,BBBB";
133+
metadata.projectId = "project-1";
134+
metadata.httpBaseUrl = "http://192.168.64.2:3773";
135+
expect(recordFaviconForThread(threadRef, favicon("http://192.168.64.2:5173/", 1))).toBe(true);
136+
137+
metadata.httpBaseUrl = null;
138+
expect(recordFaviconForThread(threadRef, favicon("http://192.168.64.2:5173/", 2, NEWER))).toBe(
139+
false,
140+
);
141+
expect(
142+
lookupFaviconForThread(
143+
useBrowserFaviconStore.getState(),
144+
scopedThreadKey(threadRef),
145+
projectRef,
146+
"http://192.168.64.2:5173/",
147+
"192.168.64.2",
148+
),
149+
).toBe(NEWER);
150+
});
151+
130152
it("keeps the memory shadow authoritative after an asymmetric primary write failure", () => {
131153
vi.stubGlobal("window", {
132154
localStorage: {

‎apps/web/src/browserFaviconStore.ts‎

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -292,20 +292,27 @@ export function useFaviconForThreadUrl(threadRef: ScopedThreadRef, url: string):
292292
? new URL(preparedConnection.value.httpBaseUrl).hostname
293293
: null;
294294
const threadKey = scopedThreadKey(threadRef);
295-
return useBrowserFaviconStore(
296-
(state) =>
297-
lookupFavicon(state.byKey, projectRef, url, environmentHostname) ??
298-
lookupPendingFavicon(state.pendingByThreadKey[threadKey], url),
295+
return useBrowserFaviconStore((state) =>
296+
lookupFaviconForThread(state, threadKey, projectRef, url, environmentHostname),
299297
);
300298
}
301299

302-
/** A capture waiting for its thread's project still shows on that thread. */
303-
function lookupPendingFavicon(
304-
pending: PendingFaviconsByOrigin | undefined,
300+
/**
301+
* A capture waiting for its thread's project still shows on that thread, and wins
302+
* over an older stored capture for the same origin.
303+
*/
304+
export function lookupFaviconForThread(
305+
state: Pick<BrowserFaviconStoreState, "byKey" | "pendingByThreadKey">,
306+
threadKey: string,
307+
projectRef: ScopedProjectRef | null,
305308
url: string,
309+
environmentHostname: string | null,
306310
): string | null {
307-
const originKey = pending ? pendingOriginKey(url) : null;
308-
return originKey ? (pending?.[originKey]?.dataUrl ?? null) : null;
311+
const stored = lookupFaviconEntry(state.byKey, projectRef, url, environmentHostname);
312+
const originKey = pendingOriginKey(url);
313+
const pending = originKey ? state.pendingByThreadKey[threadKey]?.[originKey] : undefined;
314+
if (pending && (!stored || pending.capturedAt > stored.capturedAt)) return pending.dataUrl;
315+
return stored?.dataUrl ?? null;
309316
}
310317

311318
export function lookupFavicon(
@@ -314,17 +321,26 @@ export function lookupFavicon(
314321
url: string,
315322
environmentHostname: string | null,
316323
): string | null {
324+
return lookupFaviconEntry(byKey, projectRef, url, environmentHostname)?.dataUrl ?? null;
325+
}
326+
327+
function lookupFaviconEntry(
328+
byKey: Record<string, BrowserFaviconEntry>,
329+
projectRef: ScopedProjectRef | null,
330+
url: string,
331+
environmentHostname: string | null,
332+
): BrowserFaviconEntry | null {
317333
const key = projectRef
318334
? faviconKey(scopedProjectKey(projectRef), url, environmentHostname)
319335
: null;
320336
if (!key) return null;
321337
const direct = byKey[key];
322-
if (direct) return direct.dataUrl;
338+
if (direct) return direct;
323339
try {
324340
const requestedHost = normalizeHostname(new URL(url).hostname);
325341
const localKey = faviconKey(scopedProjectKey(projectRef!), url, requestedHost);
326342
const localEntry = localKey ? byKey[localKey] : null;
327-
return localEntry?.aliases?.includes(requestedHost) ? localEntry.dataUrl : null;
343+
return localEntry?.aliases?.includes(requestedHost) ? localEntry : null;
328344
} catch {
329345
return null;
330346
}

0 commit comments

Comments
 (0)