Skip to content

Commit 1d77cec

Browse files
fix: cache project favicons across web and mobile (pingdotgg#4767)
1 parent 4ba4871 commit 1d77cec

9 files changed

Lines changed: 552 additions & 48 deletions

File tree

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

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
import { SymbolView } from "./AppSymbol";
22
import { Image } from "expo-image";
3-
import { useState } from "react";
3+
import { useLayoutEffect, useMemo, useState } from "react";
44
import { View } from "react-native";
55
import type { EnvironmentId } from "@t3tools/contracts";
6-
import { isProjectFaviconFallbackUrl } from "@t3tools/shared/projectFavicon";
6+
import {
7+
getProjectFaviconCacheKey,
8+
isProjectFaviconFallbackUrl,
9+
} from "@t3tools/shared/projectFavicon";
710
import { useThemeColor } from "../lib/useThemeColor";
811
import { useAssetUrl } from "../state/assets";
9-
10-
/* ─── Favicon cache (matches web pattern) ────────────────────────────── */
11-
const loadedFaviconUrls = new Set<string>();
12+
import {
13+
beginProjectFaviconRequest,
14+
createProjectFaviconRequest,
15+
hasLoadedProjectFavicon,
16+
markProjectFaviconFailed,
17+
markProjectFaviconLoaded,
18+
} from "./projectFaviconCache";
1219

1320
/* ─── Component ──────────────────────────────────────────────────────── */
1421
export function ProjectFavicon(props: {
@@ -26,10 +33,15 @@ export function ProjectFavicon(props: {
2633
: { _tag: "project-favicon", cwd: props.workspaceRoot },
2734
);
2835
const renderableFaviconUrl = isProjectFaviconFallbackUrl(faviconUrl) ? null : faviconUrl;
36+
const cacheKey =
37+
renderableFaviconUrl && props.workspaceRoot
38+
? getProjectFaviconCacheKey(props.environmentId, props.workspaceRoot, renderableFaviconUrl)
39+
: null;
2940

3041
return (
3142
<ProjectFaviconImage
32-
key={faviconUrl}
43+
key={cacheKey}
44+
cacheKey={cacheKey}
3345
faviconUrl={renderableFaviconUrl}
3446
open={props.open}
3547
projectTitle={props.projectTitle}
@@ -39,18 +51,32 @@ export function ProjectFavicon(props: {
3951
}
4052

4153
function ProjectFaviconImage(props: {
54+
readonly cacheKey: string | null;
4255
readonly faviconUrl: string | null;
4356
readonly open?: boolean;
4457
readonly projectTitle: string;
4558
readonly size: number;
4659
}) {
4760
const iconMuted = useThemeColor("--color-icon-subtle");
61+
const faviconRequest = useMemo(
62+
() => createProjectFaviconRequest(props.cacheKey, props.faviconUrl),
63+
[props.cacheKey, props.faviconUrl],
64+
);
65+
const [activeFaviconRequest, setActiveFaviconRequest] = useState<typeof faviconRequest>(null);
66+
useLayoutEffect(() => {
67+
if (faviconRequest === null) return;
68+
69+
const endRequest = beginProjectFaviconRequest(faviconRequest);
70+
setActiveFaviconRequest(faviconRequest);
71+
return endRequest;
72+
}, [faviconRequest]);
4873

4974
const [status, setStatus] = useState<"loading" | "loaded" | "error">(() =>
50-
props.faviconUrl && loadedFaviconUrls.has(props.faviconUrl) ? "loaded" : "loading",
75+
hasLoadedProjectFavicon(props.cacheKey) ? "loaded" : "loading",
5176
);
5277

53-
const showImage = props.faviconUrl !== null && status === "loaded";
78+
const requestIsActive = faviconRequest !== null && activeFaviconRequest === faviconRequest;
79+
const showImage = requestIsActive && status === "loaded";
5480

5581
return (
5682
<View
@@ -72,11 +98,15 @@ function ProjectFaviconImage(props: {
7298
) : null}
7399

74100
{/* Favicon image (hidden until loaded) */}
75-
{props.faviconUrl ? (
101+
{requestIsActive ? (
76102
<Image
103+
key={faviconRequest.faviconUrl}
77104
source={{
78-
uri: props.faviconUrl,
105+
uri: faviconRequest.faviconUrl,
106+
cacheKey: faviconRequest.cacheKey,
79107
}}
108+
cachePolicy="memory-disk"
109+
recyclingKey={faviconRequest.cacheKey}
80110
accessibilityLabel={`${props.projectTitle} favicon`}
81111
style={{
82112
width: props.size,
@@ -86,10 +116,13 @@ function ProjectFaviconImage(props: {
86116
}}
87117
contentFit="contain"
88118
onLoad={() => {
89-
if (props.faviconUrl) loadedFaviconUrls.add(props.faviconUrl);
119+
if (!markProjectFaviconLoaded(faviconRequest)) return;
90120
setStatus("loaded");
91121
}}
92-
onError={() => setStatus("error")}
122+
onError={() => {
123+
if (!markProjectFaviconFailed(faviconRequest)) return;
124+
setStatus("error");
125+
}}
93126
/>
94127
) : null}
95128
</View>
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import {
4+
beginProjectFaviconRequest,
5+
createProjectFaviconRequest,
6+
hasLoadedProjectFavicon,
7+
markProjectFaviconFailed,
8+
markProjectFaviconLoaded,
9+
} from "./projectFaviconCache";
10+
11+
describe("project favicon cache", () => {
12+
it("ignores callbacks from a superseded URL", () => {
13+
const cacheKey = "environment-1:/workspace:v1-favicon.svg";
14+
const expiredUrl = "https://environment.example/api/assets/expired/v1-favicon.svg";
15+
const refreshedUrl = "https://environment.example/api/assets/refreshed/v1-favicon.svg";
16+
17+
const expiredRequest = createProjectFaviconRequest(cacheKey, expiredUrl);
18+
const endExpiredRequest = beginProjectFaviconRequest(expiredRequest);
19+
markProjectFaviconLoaded(expiredRequest);
20+
const refreshedRequest = createProjectFaviconRequest(cacheKey, refreshedUrl);
21+
const endRefreshedRequest = beginProjectFaviconRequest(refreshedRequest);
22+
23+
expect(markProjectFaviconLoaded(expiredRequest)).toBe(false);
24+
expect(markProjectFaviconFailed(expiredRequest)).toBe(false);
25+
expect(hasLoadedProjectFavicon(cacheKey)).toBe(true);
26+
expect(markProjectFaviconFailed(refreshedRequest)).toBe(true);
27+
expect(hasLoadedProjectFavicon(cacheKey)).toBe(false);
28+
29+
endRefreshedRequest();
30+
endExpiredRequest();
31+
});
32+
33+
it("evicts the URL that actually failed", () => {
34+
const cacheKey = "environment-1:/workspace:v2-favicon.svg";
35+
const faviconUrl = "https://environment.example/api/assets/current/v2-favicon.svg";
36+
const request = createProjectFaviconRequest(cacheKey, faviconUrl);
37+
const endRequest = beginProjectFaviconRequest(request);
38+
39+
markProjectFaviconLoaded(request);
40+
41+
expect(markProjectFaviconFailed(request)).toBe(true);
42+
expect(hasLoadedProjectFavicon(cacheKey)).toBe(false);
43+
44+
endRequest();
45+
});
46+
47+
it("does not supersede a request until the next request begins", () => {
48+
const cacheKey = "environment-1:/workspace:v3-favicon.svg";
49+
const committedUrl = "https://environment.example/api/assets/current/v3-favicon.svg";
50+
const abandonedUrl = "https://environment.example/api/assets/abandoned/v3-favicon.svg";
51+
const committedRequest = createProjectFaviconRequest(cacheKey, committedUrl);
52+
const endCommittedRequest = beginProjectFaviconRequest(committedRequest);
53+
54+
createProjectFaviconRequest(cacheKey, abandonedUrl);
55+
56+
expect(markProjectFaviconLoaded(committedRequest)).toBe(true);
57+
expect(hasLoadedProjectFavicon(cacheKey)).toBe(true);
58+
59+
endCommittedRequest();
60+
});
61+
62+
it("requires a cache key before creating a URL-bearing request", () => {
63+
const firstUrl = "https://environment.example/api/assets/first/favicon.svg";
64+
const secondUrl = "https://environment.example/api/assets/second/favicon.svg";
65+
66+
expect(createProjectFaviconRequest(null, firstUrl)).toBeNull();
67+
expect(createProjectFaviconRequest(null, secondUrl)).toBeNull();
68+
});
69+
70+
it("restores the remaining active URL when a newer request ends", () => {
71+
const cacheKey = "environment-1:/workspace:v4-favicon.svg";
72+
const firstRequest = createProjectFaviconRequest(
73+
cacheKey,
74+
"https://environment.example/api/assets/first/v4-favicon.svg",
75+
);
76+
const secondRequest = createProjectFaviconRequest(
77+
cacheKey,
78+
"https://environment.example/api/assets/second/v4-favicon.svg",
79+
);
80+
const endFirstRequest = beginProjectFaviconRequest(firstRequest);
81+
const endSecondRequest = beginProjectFaviconRequest(secondRequest);
82+
83+
expect(markProjectFaviconLoaded(firstRequest)).toBe(false);
84+
endSecondRequest();
85+
expect(markProjectFaviconLoaded(firstRequest)).toBe(true);
86+
endFirstRequest();
87+
expect(markProjectFaviconLoaded(firstRequest)).toBe(false);
88+
});
89+
90+
it("bounds remembered loaded revisions", () => {
91+
const firstCacheKey = "environment-1:/workspace:revision-0";
92+
let lastCacheKey = firstCacheKey;
93+
94+
for (let revision = 0; revision < 300; revision++) {
95+
lastCacheKey = `environment-1:/workspace:revision-${revision}`;
96+
const request = createProjectFaviconRequest(
97+
lastCacheKey,
98+
`https://environment.example/api/assets/revision-${revision}/favicon.svg`,
99+
);
100+
const endRequest = beginProjectFaviconRequest(request);
101+
markProjectFaviconLoaded(request);
102+
endRequest();
103+
}
104+
105+
expect(hasLoadedProjectFavicon(firstCacheKey)).toBe(false);
106+
expect(hasLoadedProjectFavicon(lastCacheKey)).toBe(true);
107+
});
108+
});
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
export interface ProjectFaviconRequest {
2+
readonly cacheKey: string;
3+
readonly faviconUrl: string;
4+
}
5+
6+
interface ActiveFaviconRequests {
7+
readonly urls: Map<string, number>;
8+
currentUrl: string;
9+
}
10+
11+
const MAX_LOADED_FAVICONS = 256;
12+
const activeFaviconRequests = new Map<string, ActiveFaviconRequests>();
13+
const loadedFaviconKeys = new Map<string, true>();
14+
15+
export function createProjectFaviconRequest(
16+
cacheKey: string,
17+
faviconUrl: string,
18+
): ProjectFaviconRequest;
19+
export function createProjectFaviconRequest(
20+
cacheKey: string | null,
21+
faviconUrl: string | null,
22+
): ProjectFaviconRequest | null;
23+
export function createProjectFaviconRequest(cacheKey: string | null, faviconUrl: string | null) {
24+
if (!cacheKey || !faviconUrl) return null;
25+
return { cacheKey, faviconUrl };
26+
}
27+
28+
export function beginProjectFaviconRequest(request: ProjectFaviconRequest) {
29+
let activeRequests = activeFaviconRequests.get(request.cacheKey);
30+
if (!activeRequests) {
31+
activeRequests = { currentUrl: request.faviconUrl, urls: new Map() };
32+
activeFaviconRequests.set(request.cacheKey, activeRequests);
33+
}
34+
35+
const activeCount = activeRequests.urls.get(request.faviconUrl) ?? 0;
36+
activeRequests.urls.delete(request.faviconUrl);
37+
activeRequests.urls.set(request.faviconUrl, activeCount + 1);
38+
activeRequests.currentUrl = request.faviconUrl;
39+
40+
let ended = false;
41+
return () => {
42+
if (ended) return;
43+
ended = true;
44+
45+
const remainingCount = (activeRequests.urls.get(request.faviconUrl) ?? 1) - 1;
46+
if (remainingCount > 0) {
47+
activeRequests.urls.set(request.faviconUrl, remainingCount);
48+
return;
49+
}
50+
51+
activeRequests.urls.delete(request.faviconUrl);
52+
if (activeRequests.urls.size === 0) {
53+
if (activeFaviconRequests.get(request.cacheKey) === activeRequests) {
54+
activeFaviconRequests.delete(request.cacheKey);
55+
}
56+
return;
57+
}
58+
59+
if (activeRequests.currentUrl === request.faviconUrl) {
60+
activeRequests.currentUrl = Array.from(activeRequests.urls.keys()).at(-1)!;
61+
}
62+
};
63+
}
64+
65+
export function hasLoadedProjectFavicon(cacheKey: string | null) {
66+
return cacheKey !== null && loadedFaviconKeys.has(cacheKey);
67+
}
68+
69+
function isCurrentProjectFaviconRequest(request: ProjectFaviconRequest) {
70+
return activeFaviconRequests.get(request.cacheKey)?.currentUrl === request.faviconUrl;
71+
}
72+
73+
function rememberLoadedProjectFavicon(cacheKey: string) {
74+
loadedFaviconKeys.delete(cacheKey);
75+
loadedFaviconKeys.set(cacheKey, true);
76+
77+
if (loadedFaviconKeys.size > MAX_LOADED_FAVICONS) {
78+
loadedFaviconKeys.delete(loadedFaviconKeys.keys().next().value!);
79+
}
80+
}
81+
82+
export function markProjectFaviconLoaded(request: ProjectFaviconRequest) {
83+
if (!isCurrentProjectFaviconRequest(request)) return false;
84+
85+
rememberLoadedProjectFavicon(request.cacheKey);
86+
return true;
87+
}
88+
89+
export function markProjectFaviconFailed(request: ProjectFaviconRequest) {
90+
if (!isCurrentProjectFaviconRequest(request)) return false;
91+
92+
loadedFaviconKeys.delete(request.cacheKey);
93+
return true;
94+
}

0 commit comments

Comments
 (0)