Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: better icon caching (#5791)

Myles Scolnickf93da8884e30
diff --git a/frontend/public/circle-check.ico b/frontend/src/assets/circle-check.ico
similarity index 100%
rename from frontend/public/circle-check.ico
rename to frontend/src/assets/circle-check.ico
diff --git a/frontend/public/circle-play.ico b/frontend/src/assets/circle-play.ico
similarity index 100%
rename from frontend/public/circle-play.ico
rename to frontend/src/assets/circle-play.ico
diff --git a/frontend/public/circle-x.ico b/frontend/src/assets/circle-x.ico
similarity index 100%
rename from frontend/public/circle-x.ico
rename to frontend/src/assets/circle-x.ico
diff --git a/frontend/src/assets/icon-16x16.png b/frontend/src/assets/icon-16x16.png
new file mode 100644
index 0000000000..10de32630d
Binary files /dev/null and b/frontend/src/assets/icon-16x16.png differ
diff --git a/frontend/src/assets/icon-32x32.png b/frontend/src/assets/icon-32x32.png
new file mode 100644
index 0000000000..21d6f7a9ce
Binary files /dev/null and b/frontend/src/assets/icon-32x32.png differ
diff --git a/frontend/src/components/editor/__tests__/dynamic-favicon.test.tsx b/frontend/src/components/editor/__tests__/dynamic-favicon.test.tsx
index f907cc75bb..3ad04a894c 100644
--- a/frontend/src/components/editor/__tests__/dynamic-favicon.test.tsx
+++ b/frontend/src/components/editor/__tests__/dynamic-favicon.test.tsx
@@ -12,9 +12,6 @@ vi.mock("@/core/cells/cells", () => ({
describe("DynamicFavicon", () => {
let favicon: HTMLLinkElement;
- let mockFetch: ReturnType<typeof vi.fn>;
- let mockCreateObjectURL: ReturnType<typeof vi.fn>;
- let mockRevokeObjectURL: ReturnType<typeof vi.fn>;
beforeEach(() => {
// Mock favicon element
@@ -23,18 +20,6 @@ describe("DynamicFavicon", () => {
favicon.href = "./favicon.ico";
document.head.append(favicon);
- // Mock fetch
- mockFetch = vi.fn().mockResolvedValue({
- blob: () => new Blob(),
- });
- global.fetch = mockFetch;
-
- // Mock URL methods
- mockCreateObjectURL = vi.fn().mockReturnValue("blob:mock-url");
- mockRevokeObjectURL = vi.fn();
- global.URL.createObjectURL = mockCreateObjectURL;
- global.URL.revokeObjectURL = mockRevokeObjectURL;
-
// Mock document.hasFocus
vi.spyOn(document, "hasFocus").mockReturnValue(true);
@@ -51,18 +36,73 @@ describe("DynamicFavicon", () => {
it("should update favicon when running state changes", async () => {
render(<DynamicFavicon isRunning={true} />);
- expect(mockFetch).toHaveBeenCalledWith("./circle-play.ico");
+ // Wait for async favicon update
+ await new Promise((resolve) => setTimeout(resolve, 0));
+
+ const faviconElement =
+ document.querySelector<HTMLLinkElement>("link[rel~='icon']")!;
+ expect(faviconElement.href.endsWith("circle-play.ico")).toBe(true);
+ });
+
+ it("should show success favicon when run completes without errors", async () => {
+ const { rerender } = render(<DynamicFavicon isRunning={true} />);
+
+ // Wait for the running favicon to be set
+ await new Promise((resolve) => setTimeout(resolve, 0));
+
+ rerender(<DynamicFavicon isRunning={false} />);
+
+ // Wait for async favicon update
+ await new Promise((resolve) => setTimeout(resolve, 0));
+
+ const faviconElement =
+ document.querySelector<HTMLLinkElement>("link[rel~='icon']")!;
+ expect(faviconElement.href.endsWith("circle-check.ico")).toBe(true);
+ });
+
+ it("should show error favicon when run completes with errors", async () => {
+ (useCellErrors as ReturnType<typeof vi.fn>).mockReturnValue([
+ { error: "mock error" },
+ ]);
+
+ const { rerender } = render(<DynamicFavicon isRunning={true} />);
+
+ // Wait for the running favicon to be set
+ await new Promise((resolve) => setTimeout(resolve, 0));
+
+ rerender(<DynamicFavicon isRunning={false} />);
+
+ // Wait for async favicon update
+ await new Promise((resolve) => setTimeout(resolve, 0));
+
+ const faviconElement =
+ document.querySelector<HTMLLinkElement>("link[rel~='icon']")!;
+ expect(faviconElement.href.endsWith("circle-x.ico")).toBe(true);
});
it("should not reset favicon when not in focus", async () => {
vi.spyOn(document, "hasFocus").mockReturnValue(false);
vi.useFakeTimers();
- render(<DynamicFavicon isRunning={false} />);
- vi.clearAllMocks();
+ const { rerender } = render(<DynamicFavicon isRunning={true} />);
+
+ // Wait for the running favicon to be set
+ await vi.advanceTimersByTimeAsync(0);
+
+ rerender(<DynamicFavicon isRunning={false} />);
+
+ // Wait for async favicon update
+ await vi.advanceTimersByTimeAsync(0);
+
+ const faviconElement =
+ document.querySelector<HTMLLinkElement>("link[rel~='icon']")!;
+ expect(faviconElement.href.endsWith("circle-check.ico")).toBe(true);
+
+ // Advance timers beyond the 3-second reset timeout
await vi.advanceTimersByTimeAsync(3000);
- expect(mockFetch).not.toHaveBeenCalledWith("./favicon.ico");
+ // Favicon should still be the success one since document is not in focus
+ expect(faviconElement.href.endsWith("circle-check.ico")).toBe(true);
});
it("should create favicon link if none exists", () => {
@@ -73,13 +113,6 @@ describe("DynamicFavicon", () => {
expect(newFavicon).not.toBeNull();
});
- it("should cleanup object URLs on unmount", () => {
- const { unmount } = render(<DynamicFavicon isRunning={true} />);
- unmount();
-
- expect(mockRevokeObjectURL).toHaveBeenCalled();
- });
-
describe("notifications", () => {
beforeEach(() => {
vi.spyOn(document, "visibilityState", "get").mockReturnValue("hidden");
@@ -95,7 +128,27 @@ describe("DynamicFavicon", () => {
expect(title).toBe("Execution completed");
expect(options).toEqual({
body: "Your notebook run completed successfully.",
- icon: expect.any(String),
+ icon: "/src/assets/circle-check.ico",
+ });
+ });
+ // @ts-expect-error ok in tests
+ global.Notification.permission = "granted";
+
+ const { rerender } = render(<DynamicFavicon isRunning={true} />);
+ rerender(<DynamicFavicon isRunning={false} />);
+ });
+
+ it("should send error notification when run completes with errors", () => {
+ (useCellErrors as ReturnType<typeof vi.fn>).mockReturnValue([
+ { error: "mock error" },
+ ]);
+
+ // @ts-expect-error ok in tests
+ global.Notification = vi.fn().mockImplementation((title, options) => {
+ expect(title).toBe("Execution failed");
+ expect(options).toEqual({
+ body: "Your notebook run encountered 1 error(s).",
+ icon: "/src/assets/circle-x.ico",
});
});
// @ts-expect-error ok in tests
diff --git a/frontend/src/components/editor/dynamic-favicon.tsx b/frontend/src/components/editor/dynamic-favicon.tsx
index eb4dc884a7..6946c2c07f 100644
--- a/frontend/src/components/editor/dynamic-favicon.tsx
+++ b/frontend/src/components/editor/dynamic-favicon.tsx
@@ -4,29 +4,22 @@ import { usePrevious } from "@dnd-kit/utilities";
import { useEffect } from "react";
import { useCellErrors } from "@/core/cells/cells";
import { useEventListener } from "@/hooks/useEventListener";
+import successFaviconUrl from "../../assets/circle-check.ico";
+import runningFaviconUrl from "../../assets/circle-play.ico";
+import errorFaviconUrl from "../../assets/circle-x.ico";
const FAVICON_PATHS = {
idle: "./favicon.ico",
- success: "./circle-check.ico",
- running: "./circle-play.ico",
- error: "./circle-x.ico",
+ success: successFaviconUrl,
+ running: runningFaviconUrl,
+ error: errorFaviconUrl,
} as const;
// Cache favicon object URLs lazily
type FaviconKey = keyof typeof FAVICON_PATHS;
-const cache = new Map<FaviconKey, string>();
async function getFaviconUrl(key: FaviconKey): Promise<string> {
- const cached = cache.get(key);
- if (cached) {
- return cached;
- }
-
- const response = await fetch(FAVICON_PATHS[key]);
- const blob = await response.blob();
- const url = URL.createObjectURL(blob);
- cache.set(key, url);
- return url;
+ return FAVICON_PATHS[key];
}
interface Props {
@@ -80,13 +73,6 @@ export const DynamicFavicon = (props: Props) => {
document.getElementsByTagName("head")[0].append(favicon);
}
- useEffect(() => {
- // Cleanup on unmount
- return () => {
- cache.forEach((url) => URL.revokeObjectURL(url));
- };
- }, []);
-
useEffect(() => {
// No change on startup (autorun enabled or not)
// Treat the default marimo favicon as "idle"
diff --git a/frontend/src/components/editor/file-tree/file-explorer.tsx b/frontend/src/components/editor/file-tree/file-explorer.tsx
index 97d81f11ae..41d1981b7f 100644
--- a/frontend/src/components/editor/file-tree/file-explorer.tsx
+++ b/frontend/src/components/editor/file-tree/file-explorer.tsx
@@ -60,6 +60,7 @@ import { downloadBlob } from "@/utils/download";
import { openNotebook } from "@/utils/links";
import type { FilePath } from "@/utils/paths";
import { fileSplit } from "@/utils/pathUtils";
+import marimoIcon from "../../../assets/icon-32x32.png";
import { FileViewer } from "./file-viewer";
import type { RequestingTree } from "./requesting-tree";
import { openStateAtom, treeAtom } from "./state";
@@ -619,7 +620,7 @@ const Node = ({ node, style, dragHandle }: NodeRendererProps<FileInfo>) => {
>
{node.data.isMarimoFile ? (
<img
- src="./favicon.ico"
+ src={marimoIcon}
className="w-5 h-5 flex-shrink-0 mr-2 filter grayscale"
alt="Marimo"
/>
diff --git a/frontend/src/components/pages/run-page.tsx b/frontend/src/components/pages/run-page.tsx
index 17aa819535..2fe7c67ee1 100644
--- a/frontend/src/components/pages/run-page.tsx
+++ b/frontend/src/components/pages/run-page.tsx
@@ -6,7 +6,7 @@ import { Constants } from "@/core/constants";
import { RunApp } from "@/core/run-app";
import { isStaticNotebook } from "@/core/static/static-state";
import { isWasm } from "@/core/wasm/utils";
-import faviconUrl from "../../../public/favicon.ico?inline";
+import marimoIcon from "../../assets/icon-32x32.png";
import { ContextAwarePanel } from "../editor/chrome/panels/context-aware-panel/context-aware-panel";
import { PanelsWrapper } from "../editor/chrome/wrapper/panels";
import { createStorage } from "../editor/chrome/wrapper/storage";
@@ -46,7 +46,7 @@ const Watermark = () => {
className="text-sm text-[var(--grass-11)] font-bold tracking-wide transition-colors bg-[var(--grass-4)] hover:bg-[var(--grass-5)] border-t border-l border-[var(--grass-8)] px-3 py-1 rounded-tl-md flex items-center gap-2"
>
<span>made with marimo</span>
- <img src={faviconUrl} alt="marimo" className="h-4 w-auto" />
+ <img src={marimoIcon} alt="marimo" className="h-4 w-auto" />
</a>
</div>
);
diff --git a/marimo/_server/api/endpoints/assets.py b/marimo/_server/api/endpoints/assets.py
index c16f92b174..a7dc189f8f 100644
--- a/marimo/_server/api/endpoints/assets.py
+++ b/marimo/_server/api/endpoints/assets.py
@@ -149,9 +149,6 @@ def _inject_service_worker(html: str, file_key: str) -> str:
STATIC_FILES = [
r"(favicon\.ico)",
- r"(circle-check\.ico)",
- r"(circle-play\.ico)",
- r"(circle-x\.ico)",
r"(manifest\.json)",
r"(android-chrome-(192x192|512x512)\.png)",
r"(apple-touch-icon\.png)",