Skip to content
TypeScript

perf(web): skip favicon loading for custom project icons (#23569)

Victor Vogel37022730bacb
diff --git a/apps/web/src/components/ProjectFavicon.browser.test.tsx b/apps/web/src/components/ProjectFavicon.browser.test.tsx
new file mode 100644
index 00000000000..746302ef5b2
--- /dev/null
+++ b/apps/web/src/components/ProjectFavicon.browser.test.tsx
@@ -0,0 +1,57 @@
+import { RegistryContext } from "@effect/atom-react";
+import { EnvironmentId } from "@t3tools/contracts";
+import { Atom, AtomRegistry } from "effect/reactivity";
+import { expect, it, vi } from "vite-plus/test";
+
+import { render } from "../../test/render";
+import { ProjectFavicon, type ProjectFaviconProject } from "./ProjectFavicon";
+
+const state = vi.hoisted(() => ({ favicon: undefined as Atom.Atom<string | null> | undefined }));
+vi.mock(import("../state/assets"), () => ({
+ projectFaviconUrlAtom: () => {
+ if (!state.favicon) throw new Error("Favicon resource not configured");
+ return state.favicon;
+ },
+}));
+
+function setup() {
+ const activeResources = new Set<string>();
+ state.favicon = Atom.make((get) => {
+ activeResources.add("favicon");
+ get.addFinalizer(() => activeResources.delete("favicon"));
+ return null;
+ });
+ return { activeResources };
+}
+
+it("only activates the favicon resource after removing a custom icon", async () => {
+ const { activeResources } = setup();
+ const registry = AtomRegistry.make();
+ const project: ProjectFaviconProject = {
+ environmentId: EnvironmentId.make("test"),
+ workspaceRoot: "/project",
+ title: "Project",
+ faviconPath: "icon.png",
+ projectIcon: { kind: "monogram", text: "PR", color: "blue" },
+ };
+ const view = (projectIcon: ProjectFaviconProject["projectIcon"]) => (
+ <RegistryContext value={registry}>
+ <ProjectFavicon project={{ ...project, projectIcon }} />
+ </RegistryContext>
+ );
+ try {
+ await using screen = await render(view(project.projectIcon));
+ expect(activeResources.size).toBe(0);
+ await screen.rerender(view({ kind: "emoji", emoji: "🌱" }));
+ expect(activeResources.size).toBe(0);
+ await screen.rerender(view({ kind: "lucide", name: "leaf", color: "green" }));
+ expect(activeResources.size).toBe(0);
+ await screen.rerender(view(null));
+ expect(activeResources.size).toBe(1);
+ await screen.rerender(view(project.projectIcon));
+ await screen.rerender(view(null));
+ expect(activeResources.size).toBe(1);
+ } finally {
+ registry.dispose();
+ }
+});
diff --git a/apps/web/src/components/ProjectFavicon.tsx b/apps/web/src/components/ProjectFavicon.tsx
index 14a00ae8658..3545a0aa5db 100644
--- a/apps/web/src/components/ProjectFavicon.tsx
+++ b/apps/web/src/components/ProjectFavicon.tsx
@@ -38,13 +38,6 @@ export function ProjectFavicon(input: {
fallbackIcon?: ComponentType<{ className?: string }>;
}) {
const { project } = input;
- const src = useAtomValue(
- projectFaviconUrlAtom({
- environmentId: project.environmentId,
- cwd: project.workspaceRoot,
- faviconPath: project.faviconPath,
- }),
- );
if (project.projectIcon?.kind === "monogram") {
return (
<ProjectMonogram
@@ -81,6 +74,18 @@ export function ProjectFavicon(input: {
</span>
);
}
+ return <AutomaticProjectFavicon {...input} />;
+}
+
+function AutomaticProjectFavicon(input: Parameters<typeof ProjectFavicon>[0]) {
+ const { project } = input;
+ const src = useAtomValue(
+ projectFaviconUrlAtom({
+ environmentId: project.environmentId,
+ cwd: project.workspaceRoot,
+ faviconPath: project.faviconPath,
+ }),
+ );
const FallbackIcon = input.fallbackIcon ?? FolderCodeIcon;
if (!src || isProjectFaviconFallbackUrl(src)) {