Skip to content

Commit d487560

Browse files
t3dotggclaude
andcommitted
fix(clients): project icons no longer vanish or differ across machines
Icons for the same repo now come from one favicon source per repository group, the last loaded icon is remembered for the session so disconnects never blank rows to the folder glyph, and favicon assets cache as immutable since their URLs embed a content hash. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent fe281c5 commit d487560

14 files changed

Lines changed: 638 additions & 56 deletions

File tree

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

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
import { SymbolView } from "./AppSymbol";
2+
import { useAtomValue } from "@effect/atom-react";
23
import { Image } from "expo-image";
3-
import { useLayoutEffect, useMemo, useState } from "react";
4+
import { useLayoutEffect, useMemo, useState, useSyncExternalStore } from "react";
45
import { View } from "react-native";
56
import type { EnvironmentId } from "@t3tools/contracts";
7+
import { derivePhysicalProjectKeyFromPath } from "@t3tools/client-runtime/state/project-grouping";
8+
import {
9+
forgetProjectFavicon,
10+
getRememberedProjectFavicon,
11+
planProjectFaviconRender,
12+
projectFaviconMemoryVersion,
13+
rememberProjectFavicon,
14+
subscribeProjectFaviconMemory,
15+
} from "@t3tools/client-runtime/state/project-favicon";
616
import {
717
getProjectFaviconCacheKey,
818
isProjectFaviconFallbackUrl,
919
} from "@t3tools/shared/projectFavicon";
1020
import { useThemeColor } from "../lib/useThemeColor";
1121
import { useAssetUrl } from "../state/assets";
22+
import { projectFaviconSourcesAtom } from "../state/projects";
1223
import {
1324
beginProjectFaviconRequest,
1425
createProjectFaviconRequest,
@@ -18,6 +29,12 @@ import {
1829
} from "./projectFaviconCache";
1930

2031
/* ─── Component ──────────────────────────────────────────────────────── */
32+
/**
33+
* Renders a project's icon by repository identity: every row of a repo group
34+
* asks the same source environment for the same file, the last loaded icon is
35+
* remembered for the session so a dropped connection never blanks it, and the
36+
* row's own environment is the last resort before the folder glyph.
37+
*/
2138
export function ProjectFavicon(props: {
2239
readonly environmentId: EnvironmentId;
2340
readonly open?: boolean;
@@ -27,27 +44,57 @@ export function ProjectFavicon(props: {
2744
readonly faviconPath?: string | null;
2845
}) {
2946
const size = props.size ?? 42;
30-
const faviconUrl = useAssetUrl(
31-
props.environmentId,
32-
props.workspaceRoot === null || props.workspaceRoot === undefined
47+
const workspaceRoot = props.workspaceRoot ?? null;
48+
const physicalKey =
49+
workspaceRoot === null
50+
? null
51+
: derivePhysicalProjectKeyFromPath(props.environmentId, workspaceRoot);
52+
const faviconSources = useAtomValue(projectFaviconSourcesAtom);
53+
const source = (physicalKey === null ? null : faviconSources.get(physicalKey)) ?? null;
54+
const iconKey = source?.iconKey ?? physicalKey;
55+
56+
const faviconResource = (cwd: string, faviconPath: string | null) =>
57+
({
58+
_tag: "project-favicon",
59+
cwd,
60+
...(faviconPath ? { path: faviconPath } : {}),
61+
}) as const;
62+
const sourceUrl = useAssetUrl(
63+
source?.environmentId ?? props.environmentId,
64+
workspaceRoot === null
3365
? null
34-
: {
35-
_tag: "project-favicon",
36-
cwd: props.workspaceRoot,
37-
...(props.faviconPath ? { path: props.faviconPath } : {}),
38-
},
66+
: faviconResource(
67+
source?.cwd ?? workspaceRoot,
68+
source ? source.faviconPath : (props.faviconPath ?? null),
69+
),
3970
);
40-
const renderableFaviconUrl = isProjectFaviconFallbackUrl(faviconUrl) ? null : faviconUrl;
41-
const cacheKey =
42-
renderableFaviconUrl && props.workspaceRoot
43-
? getProjectFaviconCacheKey(props.environmentId, props.workspaceRoot, renderableFaviconUrl)
71+
const ownUrl = useAssetUrl(
72+
props.environmentId,
73+
workspaceRoot === null ? null : faviconResource(workspaceRoot, props.faviconPath ?? null),
74+
);
75+
useSyncExternalStore(subscribeProjectFaviconMemory, projectFaviconMemoryVersion);
76+
77+
const liveOf = (url: string | null, environmentId: EnvironmentId, cwd: string | null) =>
78+
url !== null && cwd !== null && !isProjectFaviconFallbackUrl(url)
79+
? { url, cacheKey: getProjectFaviconCacheKey(environmentId, cwd, url) }
4480
: null;
81+
const plan = planProjectFaviconRender({
82+
sourceLive: liveOf(
83+
sourceUrl,
84+
source?.environmentId ?? props.environmentId,
85+
source?.cwd ?? workspaceRoot,
86+
),
87+
ownLive: liveOf(ownUrl, props.environmentId, workspaceRoot),
88+
remembered: iconKey === null ? null : getRememberedProjectFavicon(iconKey),
89+
});
4590

4691
return (
4792
<ProjectFaviconImage
48-
key={cacheKey}
49-
cacheKey={cacheKey}
50-
faviconUrl={renderableFaviconUrl}
93+
key={plan?.cacheKey ?? null}
94+
cacheKey={plan?.cacheKey ?? null}
95+
iconKey={iconKey}
96+
faviconUrl={plan?.url ?? null}
97+
remember={plan?.remember ?? false}
5198
open={props.open}
5299
projectTitle={props.projectTitle}
53100
size={size}
@@ -57,7 +104,9 @@ export function ProjectFavicon(props: {
57104

58105
function ProjectFaviconImage(props: {
59106
readonly cacheKey: string | null;
107+
readonly iconKey: string | null;
60108
readonly faviconUrl: string | null;
109+
readonly remember: boolean;
61110
readonly open?: boolean;
62111
readonly projectTitle: string;
63112
readonly size: number;
@@ -122,10 +171,19 @@ function ProjectFaviconImage(props: {
122171
contentFit="contain"
123172
onLoad={() => {
124173
if (!markProjectFaviconLoaded(faviconRequest)) return;
174+
if (props.remember && props.iconKey !== null) {
175+
rememberProjectFavicon(props.iconKey, {
176+
url: faviconRequest.faviconUrl,
177+
cacheKey: faviconRequest.cacheKey,
178+
});
179+
}
125180
setStatus("loaded");
126181
}}
127182
onError={() => {
128183
if (!markProjectFaviconFailed(faviconRequest)) return;
184+
if (props.iconKey !== null) {
185+
forgetProjectFavicon(props.iconKey, faviconRequest.faviconUrl);
186+
}
129187
setStatus("error");
130188
}}
131189
/>

‎apps/mobile/src/state/projects.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { createEnvironmentProjectAtoms } from "@t3tools/client-runtime/state/projects";
22
import { createProjectEnvironmentAtoms } from "@t3tools/client-runtime/state/projects";
3+
import { selectProjectFaviconSources } from "@t3tools/client-runtime/state/project-favicon";
4+
import { Atom } from "effect/unstable/reactivity";
35

46
import { environmentCatalog } from "../connection/catalog";
57
import { connectionAtomRuntime } from "../connection/runtime";
@@ -10,3 +12,8 @@ export const environmentProjects = createEnvironmentProjectAtoms({
1012
catalogValueAtom: environmentCatalog.catalogValueAtom,
1113
snapshotAtom: environmentSnapshotAtom,
1214
});
15+
16+
/** One favicon source per repository group, keyed by physical project key. */
17+
export const projectFaviconSourcesAtom = Atom.make((get) =>
18+
selectProjectFaviconSources(get(environmentProjects.projectsAtom), null),
19+
).pipe(Atom.withLabel("mobile-project-favicon-sources"));

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@ describe("AssetAccess", () => {
6161
expect(yield* resolveAsset(token, "report.html")).toEqual({
6262
kind: "file",
6363
path: canonicalHtmlPath,
64+
cache: "default",
6465
});
6566
expect(yield* resolveAsset(token, "report.css")).toEqual({
6667
kind: "file",
6768
path: canonicalCssPath,
69+
cache: "default",
6870
});
6971
expect(yield* resolveAsset(token, "../secret.txt")).toBeNull();
7072
expect(yield* resolveAsset(token, ".env")).toBeNull();
@@ -178,6 +180,7 @@ describe("AssetAccess", () => {
178180
expect(yield* resolveAsset(token, "icon.png")).toEqual({
179181
kind: "file",
180182
path: canonicalImagePath,
183+
cache: "default",
181184
});
182185
expect(yield* resolveAsset(token, "other.png")).toBeNull();
183186
expect(yield* resolveAsset(token, "../icon.png")).toBeNull();
@@ -204,6 +207,7 @@ describe("AssetAccess", () => {
204207
expect(yield* resolveAsset(token, "ignored.png")).toEqual({
205208
kind: "file",
206209
path: attachmentPath,
210+
cache: "default",
207211
});
208212
}).pipe(Effect.provide(testLayer)),
209213
);
@@ -239,7 +243,7 @@ describe("AssetAccess", () => {
239243
faviconSuffix.slice(0, faviconSeparatorIndex),
240244
faviconSuffix.slice(faviconSeparatorIndex + 1),
241245
),
242-
).toEqual({ kind: "file", path: canonicalFaviconPath });
246+
).toEqual({ kind: "file", path: canonicalFaviconPath, cache: "immutable" });
243247

244248
yield* fileSystem.writeFileString(faviconPath, updatedFavicon);
245249
const updatedFaviconResult = yield* issueAssetUrl({

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,12 @@ const AssetClaimsJson = Schema.fromJsonString(AssetClaimsSchema);
9595
const decodeAssetClaims = Schema.decodeUnknownOption(AssetClaimsJson);
9696
const encodeAssetClaims = Schema.encodeSync(AssetClaimsJson);
9797

98-
export type ResolvedAsset = { readonly kind: "file"; readonly path: string };
98+
export type ResolvedAsset = {
99+
readonly kind: "file";
100+
readonly path: string;
101+
/** Immutable assets embed a content hash in their URL and cache long-term. */
102+
readonly cache: "default" | "immutable";
103+
};
99104

100105
function decodeClaims(encodedPayload: string): AssetClaims | null {
101106
try {
@@ -428,7 +433,7 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* (
428433
Effect.orElseSucceed(() => Option.none()),
429434
);
430435
return Option.isSome(info) && info.value.type === "File"
431-
? ({ kind: "file", path: attachmentPath } satisfies ResolvedAsset)
436+
? ({ kind: "file", path: attachmentPath, cache: "default" } satisfies ResolvedAsset)
432437
: null;
433438
}
434439

@@ -438,7 +443,9 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* (
438443
workspaceRoot: claims.workspaceRoot,
439444
relativePath: claims.relativePath,
440445
});
441-
return faviconPath ? ({ kind: "file", path: faviconPath } satisfies ResolvedAsset) : null;
446+
return faviconPath
447+
? ({ kind: "file", path: faviconPath, cache: "immutable" } satisfies ResolvedAsset)
448+
: null;
442449
}
443450

444451
const decodedPath = decodeRelativePath(relativePath);
@@ -451,7 +458,7 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* (
451458
relativePath: claims.relativePath,
452459
});
453460
return exactWorkspaceFile
454-
? ({ kind: "file", path: exactWorkspaceFile } satisfies ResolvedAsset)
461+
? ({ kind: "file", path: exactWorkspaceFile, cache: "default" } satisfies ResolvedAsset)
455462
: null;
456463
}
457464
const segments = decodedPath.split(/[\\/]/);
@@ -469,5 +476,7 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* (
469476
workspaceRoot: claims.workspaceRoot,
470477
relativePath: joinedRelativePath,
471478
});
472-
return workspaceFile ? ({ kind: "file", path: workspaceFile } satisfies ResolvedAsset) : null;
479+
return workspaceFile
480+
? ({ kind: "file", path: workspaceFile, cache: "default" } satisfies ResolvedAsset)
481+
: null;
473482
});

‎apps/server/src/http.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,11 @@ describe("assetResponseHeaders", () => {
4444
"X-Content-Type-Options": "nosniff",
4545
});
4646
});
47+
48+
it("caches immutable assets long-term", () => {
49+
expect(assetResponseHeaders("/repo/favicon.png", "immutable")).toEqual({
50+
"Cache-Control": "private, max-age=31536000, immutable",
51+
"X-Content-Type-Options": "nosniff",
52+
});
53+
});
4754
});

‎apps/server/src/http.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,15 @@ const LOOPBACK_HOSTNAMES = new Set(["127.0.0.1", "::1", "localhost"]);
4545
const DESKTOP_RENDERER_ORIGINS = ["t3code://app", "t3code-dev://app"];
4646
const SVG_CONTENT_SECURITY_POLICY = "default-src 'none'; style-src 'unsafe-inline'; sandbox";
4747

48-
export function assetResponseHeaders(filePath: string): Record<string, string> {
48+
export function assetResponseHeaders(
49+
filePath: string,
50+
cache: "default" | "immutable" = "default",
51+
): Record<string, string> {
4952
return {
50-
"Cache-Control": "private, max-age=3600",
53+
// Immutable assets embed a content hash in their URL (project favicons),
54+
// so browsers may keep serving them while the environment is unreachable.
55+
"Cache-Control":
56+
cache === "immutable" ? "private, max-age=31536000, immutable" : "private, max-age=3600",
5157
"X-Content-Type-Options": "nosniff",
5258
...(filePath.toLowerCase().endsWith(".svg")
5359
? { "Content-Security-Policy": SVG_CONTENT_SECURITY_POLICY }
@@ -219,7 +225,7 @@ export const assetRouteLayer = HttpRouter.add(
219225
}
220226
return yield* HttpServerResponse.file(asset.path, {
221227
status: 200,
222-
headers: assetResponseHeaders(asset.path),
228+
headers: assetResponseHeaders(asset.path, asset.cache),
223229
}).pipe(
224230
Effect.orElseSucceed(() => HttpServerResponse.text("Internal Server Error", { status: 500 })),
225231
);

0 commit comments

Comments
 (0)