Skip to content

Commit f77304e

Browse files
fix: restore favicon load error fallback
1 parent 8bcd7c2 commit f77304e

2 files changed

Lines changed: 136 additions & 0 deletions

File tree

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import type { ComponentType, Dispatch, ReactElement, SetStateAction } from "react";
2+
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
3+
import type { EnvironmentId } from "@t3tools/contracts";
4+
5+
const testState = vi.hoisted(() => ({
6+
faviconUrl: "https://environment.test/api/assets/token-a/v1-20-favicon.svg",
7+
}));
8+
9+
const hooks = vi.hoisted(() => {
10+
let cursor = 0;
11+
let slots: unknown[] = [];
12+
const nextIndex = () => cursor++;
13+
14+
return {
15+
beginRender() {
16+
cursor = 0;
17+
},
18+
reset() {
19+
cursor = 0;
20+
slots = [];
21+
},
22+
useMemoCache(size: number): unknown[] {
23+
const index = nextIndex();
24+
if (!slots[index]) {
25+
slots[index] = Array.from({ length: size }, () => Symbol.for("react.memo_cache_sentinel"));
26+
}
27+
return slots[index] as unknown[];
28+
},
29+
useState<T>(initialValue: T | (() => T)): [T, Dispatch<SetStateAction<T>>] {
30+
const index = nextIndex();
31+
if (index >= slots.length) {
32+
slots[index] =
33+
typeof initialValue === "function" ? (initialValue as () => T)() : initialValue;
34+
}
35+
const setValue: Dispatch<SetStateAction<T>> = (nextValue) => {
36+
const previous = slots[index] as T;
37+
slots[index] =
38+
typeof nextValue === "function" ? (nextValue as (value: T) => T)(previous) : nextValue;
39+
};
40+
return [slots[index] as T, setValue];
41+
},
42+
};
43+
});
44+
45+
vi.mock("react", async (importOriginal) => {
46+
const actual = await importOriginal<typeof import("react")>();
47+
return {
48+
...actual,
49+
useState: hooks.useState,
50+
};
51+
});
52+
53+
vi.mock("react/compiler-runtime", () => ({ c: hooks.useMemoCache }));
54+
vi.mock("../assets/assetUrls", () => ({
55+
useAssetUrl: () => testState.faviconUrl,
56+
}));
57+
58+
import { ProjectFavicon } from "./ProjectFavicon";
59+
60+
type ProjectFaviconImageProps = {
61+
readonly cacheKey: string;
62+
readonly src: string;
63+
readonly className?: string | undefined;
64+
readonly fallbackIcon: ComponentType<{ className?: string }>;
65+
};
66+
67+
type ImageElement = ReactElement<{
68+
readonly src: string;
69+
readonly onLoad?: () => void;
70+
readonly onError?: () => void;
71+
}>;
72+
73+
type ProjectFaviconImageElement = ReactElement<{
74+
readonly children: [ReactElement | null, ImageElement | null, ImageElement | null];
75+
}>;
76+
77+
function resolveImageComponent(): {
78+
readonly Component: (props: ProjectFaviconImageProps) => ProjectFaviconImageElement;
79+
readonly props: ProjectFaviconImageProps;
80+
} {
81+
hooks.beginRender();
82+
const element = ProjectFavicon({
83+
environmentId: "environment-test" as EnvironmentId,
84+
cwd: "/workspace-test",
85+
}) as ReactElement<ProjectFaviconImageProps>;
86+
hooks.reset();
87+
88+
return {
89+
Component: element.type as (props: ProjectFaviconImageProps) => ProjectFaviconImageElement,
90+
props: element.props,
91+
};
92+
}
93+
94+
function renderImage(
95+
Component: (props: ProjectFaviconImageProps) => ProjectFaviconImageElement,
96+
props: ProjectFaviconImageProps,
97+
): ProjectFaviconImageElement {
98+
hooks.beginRender();
99+
return Component(props);
100+
}
101+
102+
describe("ProjectFavicon", () => {
103+
beforeEach(() => {
104+
hooks.reset();
105+
});
106+
107+
it("falls back when the displayed favicon fails without discarding a valid older image early", () => {
108+
const { Component, props } = resolveImageComponent();
109+
const initialLoadingImage = renderImage(Component, props).props.children[2];
110+
initialLoadingImage?.props.onLoad?.();
111+
112+
const refreshedProps = {
113+
...props,
114+
src: "https://environment.test/api/assets/token-b/v1-20-favicon.svg",
115+
};
116+
const refreshing = renderImage(Component, refreshedProps).props.children;
117+
expect(refreshing[1]?.props.src).toBe(props.src);
118+
refreshing[2]?.props.onError?.();
119+
120+
const afterRefreshError = renderImage(Component, refreshedProps).props.children;
121+
expect(afterRefreshError[1]?.props.src).toBe(props.src);
122+
afterRefreshError[1]?.props.onError?.();
123+
124+
const afterDisplayedError = renderImage(Component, refreshedProps).props.children;
125+
expect(afterDisplayedError[0]).not.toBeNull();
126+
expect(afterDisplayedError[1]).toBeNull();
127+
});
128+
});

‎apps/web/src/components/ProjectFavicon.tsx‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ function ProjectFaviconImage({
6565
() => loadedProjectFaviconSrcs.get(cacheKey) ?? null,
6666
);
6767
const isLoading = displayedSrc !== src;
68+
const handleLoadError = (failedSrc: string) => {
69+
if (loadedProjectFaviconSrcs.get(cacheKey) === failedSrc) {
70+
loadedProjectFaviconSrcs.delete(cacheKey);
71+
}
72+
setDisplayedSrc((currentSrc) => (currentSrc === failedSrc ? null : currentSrc));
73+
};
6874

6975
return (
7076
<>
@@ -76,6 +82,7 @@ function ProjectFaviconImage({
7682
src={displayedSrc}
7783
alt=""
7884
className={cn("size-3.5 shrink-0 rounded-sm object-contain", className)}
85+
onError={() => handleLoadError(displayedSrc)}
7986
/>
8087
) : null}
8188
{isLoading ? (
@@ -87,6 +94,7 @@ function ProjectFaviconImage({
8794
loadedProjectFaviconSrcs.set(cacheKey, src);
8895
setDisplayedSrc(src);
8996
}}
97+
onError={() => handleLoadError(src)}
9098
/>
9199
) : null}
92100
</>

0 commit comments

Comments
 (0)