Skip to content

Commit 3702273

Browse files
only21milcodexclaude
authored
perf(web): skip favicon loading for custom project icons (#23569)
Co-authored-by: GPT-6.1 Sol <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 0804c60 commit 3702273

2 files changed

Lines changed: 69 additions & 7 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { RegistryContext } from "@effect/atom-react";
2+
import { EnvironmentId } from "@t3tools/contracts";
3+
import { Atom, AtomRegistry } from "effect/reactivity";
4+
import { expect, it, vi } from "vite-plus/test";
5+
6+
import { render } from "../../test/render";
7+
import { ProjectFavicon, type ProjectFaviconProject } from "./ProjectFavicon";
8+
9+
const state = vi.hoisted(() => ({ favicon: undefined as Atom.Atom<string | null> | undefined }));
10+
vi.mock(import("../state/assets"), () => ({
11+
projectFaviconUrlAtom: () => {
12+
if (!state.favicon) throw new Error("Favicon resource not configured");
13+
return state.favicon;
14+
},
15+
}));
16+
17+
function setup() {
18+
const activeResources = new Set<string>();
19+
state.favicon = Atom.make((get) => {
20+
activeResources.add("favicon");
21+
get.addFinalizer(() => activeResources.delete("favicon"));
22+
return null;
23+
});
24+
return { activeResources };
25+
}
26+
27+
it("only activates the favicon resource after removing a custom icon", async () => {
28+
const { activeResources } = setup();
29+
const registry = AtomRegistry.make();
30+
const project: ProjectFaviconProject = {
31+
environmentId: EnvironmentId.make("test"),
32+
workspaceRoot: "/project",
33+
title: "Project",
34+
faviconPath: "icon.png",
35+
projectIcon: { kind: "monogram", text: "PR", color: "blue" },
36+
};
37+
const view = (projectIcon: ProjectFaviconProject["projectIcon"]) => (
38+
<RegistryContext value={registry}>
39+
<ProjectFavicon project={{ ...project, projectIcon }} />
40+
</RegistryContext>
41+
);
42+
try {
43+
await using screen = await render(view(project.projectIcon));
44+
expect(activeResources.size).toBe(0);
45+
await screen.rerender(view({ kind: "emoji", emoji: "🌱" }));
46+
expect(activeResources.size).toBe(0);
47+
await screen.rerender(view({ kind: "lucide", name: "leaf", color: "green" }));
48+
expect(activeResources.size).toBe(0);
49+
await screen.rerender(view(null));
50+
expect(activeResources.size).toBe(1);
51+
await screen.rerender(view(project.projectIcon));
52+
await screen.rerender(view(null));
53+
expect(activeResources.size).toBe(1);
54+
} finally {
55+
registry.dispose();
56+
}
57+
});

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,6 @@ export function ProjectFavicon(input: {
3838
fallbackIcon?: ComponentType<{ className?: string }>;
3939
}) {
4040
const { project } = input;
41-
const src = useAtomValue(
42-
projectFaviconUrlAtom({
43-
environmentId: project.environmentId,
44-
cwd: project.workspaceRoot,
45-
faviconPath: project.faviconPath,
46-
}),
47-
);
4841
if (project.projectIcon?.kind === "monogram") {
4942
return (
5043
<ProjectMonogram
@@ -81,6 +74,18 @@ export function ProjectFavicon(input: {
8174
</span>
8275
);
8376
}
77+
return <AutomaticProjectFavicon {...input} />;
78+
}
79+
80+
function AutomaticProjectFavicon(input: Parameters<typeof ProjectFavicon>[0]) {
81+
const { project } = input;
82+
const src = useAtomValue(
83+
projectFaviconUrlAtom({
84+
environmentId: project.environmentId,
85+
cwd: project.workspaceRoot,
86+
faviconPath: project.faviconPath,
87+
}),
88+
);
8489
const FallbackIcon = input.fallbackIcon ?? FolderCodeIcon;
8590

8691
if (!src || isProjectFaviconFallbackUrl(src)) {

0 commit comments

Comments
 (0)