Skip to content

Commit 6ca94bf

Browse files
fix: make favicon refresh ordering deterministic
1 parent fb71493 commit 6ca94bf

5 files changed

Lines changed: 86 additions & 25 deletions

File tree

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { SymbolView } from "./AppSymbol";
22
import { Image } from "expo-image";
3-
import { useState } from "react";
3+
import { useRef, useState } from "react";
44
import { View } from "react-native";
55
import type { EnvironmentId } from "@t3tools/contracts";
66
import {
@@ -10,9 +10,11 @@ import {
1010
import { useThemeColor } from "../lib/useThemeColor";
1111
import { useAssetUrl } from "../state/assets";
1212
import {
13+
beginProjectFaviconRequest,
1314
hasLoadedProjectFavicon,
1415
markProjectFaviconFailed,
1516
markProjectFaviconLoaded,
17+
type ProjectFaviconRequest,
1618
} from "./projectFaviconCache";
1719

1820
/* ─── Component ──────────────────────────────────────────────────────── */
@@ -56,6 +58,15 @@ function ProjectFaviconImage(props: {
5658
readonly size: number;
5759
}) {
5860
const iconMuted = useThemeColor("--color-icon-subtle");
61+
const faviconRequestRef = useRef<ProjectFaviconRequest | null>(null);
62+
if (
63+
faviconRequestRef.current === null ||
64+
faviconRequestRef.current.cacheKey !== props.cacheKey ||
65+
faviconRequestRef.current.faviconUrl !== props.faviconUrl
66+
) {
67+
faviconRequestRef.current = beginProjectFaviconRequest(props.cacheKey, props.faviconUrl);
68+
}
69+
const faviconRequest = faviconRequestRef.current;
5970

6071
const [status, setStatus] = useState<"loading" | "loaded" | "error">(() =>
6172
hasLoadedProjectFavicon(props.cacheKey) ? "loaded" : "loading",
@@ -101,11 +112,11 @@ function ProjectFaviconImage(props: {
101112
}}
102113
contentFit="contain"
103114
onLoad={() => {
104-
markProjectFaviconLoaded(props.cacheKey, props.faviconUrl);
115+
if (!markProjectFaviconLoaded(faviconRequest)) return;
105116
setStatus("loaded");
106117
}}
107118
onError={() => {
108-
if (!markProjectFaviconFailed(props.cacheKey, props.faviconUrl)) return;
119+
if (!markProjectFaviconFailed(faviconRequest)) return;
109120
setStatus("error");
110121
}}
111122
/>
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
11
import { describe, expect, it } from "vite-plus/test";
22

33
import {
4+
beginProjectFaviconRequest,
45
hasLoadedProjectFavicon,
56
markProjectFaviconFailed,
67
markProjectFaviconLoaded,
78
} from "./projectFaviconCache";
89

910
describe("project favicon cache", () => {
10-
it("ignores an old URL error after a refreshed URL loads", () => {
11+
it("ignores callbacks from a superseded URL", () => {
1112
const cacheKey = "environment-1:/workspace:v1-favicon.svg";
1213
const expiredUrl = "https://environment.example/api/assets/expired/v1-favicon.svg";
1314
const refreshedUrl = "https://environment.example/api/assets/refreshed/v1-favicon.svg";
1415

15-
markProjectFaviconLoaded(cacheKey, expiredUrl);
16-
markProjectFaviconLoaded(cacheKey, refreshedUrl);
16+
const expiredRequest = beginProjectFaviconRequest(cacheKey, expiredUrl);
17+
markProjectFaviconLoaded(expiredRequest);
18+
const refreshedRequest = beginProjectFaviconRequest(cacheKey, refreshedUrl);
1719

18-
expect(markProjectFaviconFailed(cacheKey, expiredUrl)).toBe(false);
20+
expect(markProjectFaviconLoaded(expiredRequest)).toBe(false);
21+
expect(markProjectFaviconFailed(expiredRequest)).toBe(false);
1922
expect(hasLoadedProjectFavicon(cacheKey)).toBe(true);
23+
expect(markProjectFaviconFailed(refreshedRequest)).toBe(true);
24+
expect(hasLoadedProjectFavicon(cacheKey)).toBe(false);
2025
});
2126

2227
it("evicts the URL that actually failed", () => {
2328
const cacheKey = "environment-1:/workspace:v2-favicon.svg";
2429
const faviconUrl = "https://environment.example/api/assets/current/v2-favicon.svg";
30+
const request = beginProjectFaviconRequest(cacheKey, faviconUrl);
2531

26-
markProjectFaviconLoaded(cacheKey, faviconUrl);
32+
markProjectFaviconLoaded(request);
2733

28-
expect(markProjectFaviconFailed(cacheKey, faviconUrl)).toBe(true);
34+
expect(markProjectFaviconFailed(request)).toBe(true);
2935
expect(hasLoadedProjectFavicon(cacheKey)).toBe(false);
3036
});
3137
});
Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
1-
const loadedFaviconUrls = new Map<string, string>();
1+
export interface ProjectFaviconRequest {
2+
readonly cacheKey: string | null;
3+
readonly faviconUrl: string | null;
4+
}
25

3-
export function hasLoadedProjectFavicon(cacheKey: string | null) {
4-
return cacheKey !== null && loadedFaviconUrls.has(cacheKey);
6+
const currentFaviconRequests = new Map<string, ProjectFaviconRequest>();
7+
const loadedFaviconKeys = new Set<string>();
8+
9+
export function beginProjectFaviconRequest(cacheKey: string | null, faviconUrl: string | null) {
10+
const currentRequest = cacheKey ? currentFaviconRequests.get(cacheKey) : undefined;
11+
if (currentRequest?.faviconUrl === faviconUrl) return currentRequest;
12+
13+
const request = { cacheKey, faviconUrl };
14+
if (cacheKey && faviconUrl) currentFaviconRequests.set(cacheKey, request);
15+
return request;
516
}
617

7-
export function markProjectFaviconLoaded(cacheKey: string | null, faviconUrl: string | null) {
8-
if (cacheKey && faviconUrl) loadedFaviconUrls.set(cacheKey, faviconUrl);
18+
export function hasLoadedProjectFavicon(cacheKey: string | null) {
19+
return cacheKey !== null && loadedFaviconKeys.has(cacheKey);
920
}
1021

11-
export function markProjectFaviconFailed(cacheKey: string | null, faviconUrl: string | null) {
12-
if (!cacheKey) return true;
22+
export function markProjectFaviconLoaded(request: ProjectFaviconRequest) {
23+
if (request.cacheKey && currentFaviconRequests.get(request.cacheKey) !== request) return false;
24+
25+
if (request.cacheKey) loadedFaviconKeys.add(request.cacheKey);
26+
return true;
27+
}
1328

14-
const loadedUrl = loadedFaviconUrls.get(cacheKey);
15-
if (loadedUrl !== undefined && loadedUrl !== faviconUrl) return false;
29+
export function markProjectFaviconFailed(request: ProjectFaviconRequest) {
30+
if (request.cacheKey && currentFaviconRequests.get(request.cacheKey) !== request) return false;
1631

17-
loadedFaviconUrls.delete(cacheKey);
32+
if (request.cacheKey) loadedFaviconKeys.delete(request.cacheKey);
1833
return true;
1934
}

‎apps/server/src/assets/AssetAccess.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
22
import { ThreadId } from "@t3tools/contracts";
33
import { PROJECT_FAVICON_FALLBACK_MARKER } from "@t3tools/shared/projectFavicon";
44
import { describe, expect, it } from "@effect/vitest";
5+
import * as Crypto from "effect/Crypto";
56
import * as Effect from "effect/Effect";
67
import * as FileSystem from "effect/FileSystem";
78
import * as Layer from "effect/Layer";
89
import * as Path from "effect/Path";
910
import * as PlatformError from "effect/PlatformError";
11+
import * as TestClock from "effect/testing/TestClock";
1012

1113
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
1214
import * as ServerConfig from "../config.ts";
@@ -262,6 +264,31 @@ describe("AssetAccess", () => {
262264
}).pipe(Effect.provide(testLayer)),
263265
);
264266

267+
it.effect("buckets project favicon expiry after content hashing", () =>
268+
Effect.gen(function* () {
269+
const crypto = yield* Crypto.Crypto;
270+
const fileSystem = yield* FileSystem.FileSystem;
271+
const path = yield* Path.Path;
272+
const root = yield* fileSystem.makeTempDirectoryScoped({
273+
prefix: "t3-asset-favicon-expiry-",
274+
});
275+
yield* fileSystem.writeFileString(path.join(root, "favicon.svg"), "<svg />");
276+
277+
const bucketMs = 30 * 60 * 1000;
278+
yield* TestClock.setTime(bucketMs - 1);
279+
const crossingCrypto = Crypto.make({
280+
randomBytes: (size) => new Uint8Array(size),
281+
digest: (algorithm, data) =>
282+
TestClock.adjust("2 millis").pipe(Effect.andThen(crypto.digest(algorithm, data))),
283+
});
284+
const result = yield* issueAssetUrl({
285+
resource: { _tag: "project-favicon", cwd: root },
286+
}).pipe(Effect.provideService(Crypto.Crypto, crossingCrypto));
287+
288+
expect(result.expiresAt).toBe(3 * bucketMs);
289+
}).pipe(Effect.provide(testLayer)),
290+
);
291+
265292
it.effect("preserves structured project favicon resolution causes", () =>
266293
Effect.gen(function* () {
267294
const fileSystem = yield* FileSystem.FileSystem;

‎apps/server/src/assets/AssetAccess.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,7 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
173173
const fileSystem = yield* FileSystem.FileSystem;
174174
const path = yield* Path.Path;
175175
const workspacePaths = yield* WorkspacePaths.WorkspacePaths;
176-
const issuedAt = yield* Clock.currentTimeMillis;
177-
const expiresAt =
178-
input.resource._tag === "project-favicon"
179-
? (Math.floor(issuedAt / PROJECT_FAVICON_TOKEN_BUCKET_MS) + 2) *
180-
PROJECT_FAVICON_TOKEN_BUCKET_MS
181-
: issuedAt + ASSET_TOKEN_TTL_MS;
176+
let expiresAt = (yield* Clock.currentTimeMillis) + ASSET_TOKEN_TTL_MS;
182177
let claims: AssetClaims;
183178
let fileName: string;
184179

@@ -372,6 +367,13 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
372367
}),
373368
),
374369
);
370+
if (claims.kind === "project-favicon") {
371+
const issuedAt = yield* Clock.currentTimeMillis;
372+
expiresAt =
373+
(Math.floor(issuedAt / PROJECT_FAVICON_TOKEN_BUCKET_MS) + 2) *
374+
PROJECT_FAVICON_TOKEN_BUCKET_MS;
375+
claims = { ...claims, expiresAt };
376+
}
375377
const encodedPayload = base64UrlEncode(encodeAssetClaims(claims));
376378
const token = `${encodedPayload}.${signPayload(encodedPayload, signingSecret)}`;
377379
return {

0 commit comments

Comments
 (0)