Skip to content

Commit 6ba92f5

Browse files
murenovichclaude
andcommitted
fix(server): favicon resolution no longer pins the event loop
`LINK_ICON_OBJ_RE` was unanchored, so it restarted at every offset in an icon source file and rescanned forward from each one. A project with no icon file and a large `index.html` that has no `<link rel="icon">` made favicon resolution spin for minutes on the server's only thread, so every connection stopped being answered and the desktop client dropped into a permanent reconnect loop. Measured on a 1.6 MB generated `index.html`: 200 KB already cost ~4s, and the full file never completed. That pattern accepted any position from which both `rel` and `href` were visible before the next `}`, which is exactly "both live in the same brace-free run". Walking those runs directly gives the same answers in linear time, and keeps working where a single anchored pattern would not: runs holding `rel` but no `href` fall through to the next candidate, and metadata sitting beside a nested object still resolves. The same 1.6 MB file now finishes in 2 ms. The object branch had no test coverage, so this adds both key orders, a nested-object case, a no-href-then-valid case, and a large brace-sparse source that hangs without the fix. Model: Claude Opus 5 · Harness: Claude Code Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent e4abc31 commit 6ba92f5

2 files changed

Lines changed: 115 additions & 4 deletions

File tree

‎apps/server/src/project/ProjectFaviconResolver.test.ts‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,109 @@ it.layer(TestLayer)("ProjectFaviconResolverLive", (it) => {
133133
}),
134134
);
135135

136+
it.effect("resolves icon hrefs from object-literal route metadata", () =>
137+
Effect.gen(function* () {
138+
const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
139+
const cwd = yield* makeTempDir;
140+
yield* writeTextFile(
141+
cwd,
142+
"src/routes/__root.tsx",
143+
`export const Route = createRootRoute({
144+
head: () => ({
145+
links: [
146+
{ rel: "stylesheet", href: "/app.css" },
147+
{ rel: "icon", href: "/brand/logo.svg" },
148+
],
149+
}),
150+
});`,
151+
);
152+
yield* writeTextFile(cwd, "public/brand/logo.svg", "<svg>brand</svg>");
153+
154+
const resolved = yield* resolver.resolvePath(cwd);
155+
156+
expect(resolved).not.toBeNull();
157+
expect(resolved).toContain("public/brand/logo.svg");
158+
}),
159+
);
160+
161+
it.effect("resolves object-literal icon metadata when href precedes rel", () =>
162+
Effect.gen(function* () {
163+
const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
164+
const cwd = yield* makeTempDir;
165+
yield* writeTextFile(
166+
cwd,
167+
"src/root.tsx",
168+
`const links = [{ href: "/brand/logo.svg", rel: "shortcut icon" }];`,
169+
);
170+
yield* writeTextFile(cwd, "public/brand/logo.svg", "<svg>brand</svg>");
171+
172+
const resolved = yield* resolver.resolvePath(cwd);
173+
174+
expect(resolved).not.toBeNull();
175+
expect(resolved).toContain("public/brand/logo.svg");
176+
}),
177+
);
178+
179+
it.effect("resolves object-literal icon metadata alongside nested objects", () =>
180+
Effect.gen(function* () {
181+
const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
182+
const cwd = yield* makeTempDir;
183+
yield* writeTextFile(
184+
cwd,
185+
"src/root.tsx",
186+
`const links = [{ attributes: {}, rel: "icon", href: "/brand/logo.svg" }];`,
187+
);
188+
yield* writeTextFile(cwd, "public/brand/logo.svg", "<svg>brand</svg>");
189+
190+
const resolved = yield* resolver.resolvePath(cwd);
191+
192+
expect(resolved).not.toBeNull();
193+
expect(resolved).toContain("public/brand/logo.svg");
194+
}),
195+
);
196+
197+
it.effect("skips icon metadata without an href and keeps scanning", () =>
198+
Effect.gen(function* () {
199+
const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
200+
const cwd = yield* makeTempDir;
201+
yield* writeTextFile(
202+
cwd,
203+
"src/root.tsx",
204+
`const links = [{ rel: "icon" }, { rel: "icon", href: "/brand/logo.svg" }];`,
205+
);
206+
yield* writeTextFile(cwd, "public/brand/logo.svg", "<svg>brand</svg>");
207+
208+
const resolved = yield* resolver.resolvePath(cwd);
209+
210+
expect(resolved).not.toBeNull();
211+
expect(resolved).toContain("public/brand/logo.svg");
212+
}),
213+
);
214+
215+
// A large icon source with no icon metadata used to pin the server's event loop for
216+
// minutes: the object pattern was unanchored, so it restarted at every offset and
217+
// rescanned forward from each one. Anchoring keeps this proportional to file size.
218+
it.effect("scans large icon sources without an icon in reasonable time", () =>
219+
Effect.gen(function* () {
220+
const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
221+
const cwd = yield* makeTempDir;
222+
// Mirrors a generated single-file build: large, brace-sparse, and no icon metadata.
223+
const filler = `<p>${"pokopia companion guide ".repeat(24)}</p>\n`;
224+
yield* writeTextFile(
225+
cwd,
226+
"index.html",
227+
`<!doctype html><html><head><title>guide</title></head><body>\n${filler.repeat(1200)}</body></html>`,
228+
);
229+
230+
const startedAt = performance.now();
231+
const resolved = yield* resolver.resolvePath(cwd);
232+
const elapsedMs = performance.now() - startedAt;
233+
234+
expect(resolved).toBeNull();
235+
expect(elapsedMs).toBeLessThan(5_000);
236+
}),
237+
);
238+
136239
it.effect("returns null when no icon is present", () =>
137240
Effect.gen(function* () {
138241
const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;

‎apps/server/src/project/ProjectFaviconResolver.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,13 @@ const ICON_SOURCE_FILES = [
5555
] as const;
5656

5757
// Matches <link ...> tags or object-like icon metadata where rel/href can appear in any order.
58+
// The tag pattern is anchored on `<link`, so it only starts at real candidates. Object metadata
59+
// is matched by scanning brace-free runs instead of by one combined pattern: an unanchored
60+
// pattern restarts at every offset and rescans forward, which is quadratic on large sources.
5861
const LINK_ICON_HTML_RE =
5962
/<link\b(?=[^>]*\brel=["'](?:icon|shortcut icon)["'])(?=[^>]*\bhref=["']([^"'?]+))[^>]*>/i;
60-
const LINK_ICON_OBJ_RE =
61-
/(?=[^}]*\brel\s*:\s*["'](?:icon|shortcut icon)["'])(?=[^}]*\bhref\s*:\s*["']([^"'?]+))[^}]*/i;
63+
const ICON_REL_RE = /\brel\s*:\s*["'](?:icon|shortcut icon)["']/i;
64+
const ICON_HREF_RE = /\bhref\s*:\s*["']([^"'?]+)/i;
6265

6366
export class ProjectFaviconResolutionError extends Schema.TaggedErrorClass<ProjectFaviconResolutionError>()(
6467
"ProjectFaviconResolutionError",
@@ -98,8 +101,13 @@ export class ProjectFaviconResolver extends Context.Service<
98101
function extractIconHref(source: string): string | null {
99102
const htmlMatch = source.match(LINK_ICON_HTML_RE);
100103
if (htmlMatch?.[1]) return htmlMatch[1];
101-
const objMatch = source.match(LINK_ICON_OBJ_RE);
102-
if (objMatch?.[1]) return objMatch[1];
104+
// Icon metadata counts when `rel` and `href` share a brace-free run, so a run holding `rel`
105+
// but no href falls through to the next one rather than ending the search.
106+
for (const run of source.split("}")) {
107+
if (!ICON_REL_RE.test(run)) continue;
108+
const hrefMatch = run.match(ICON_HREF_RE);
109+
if (hrefMatch?.[1]) return hrefMatch[1];
110+
}
103111
return null;
104112
}
105113

0 commit comments

Comments
 (0)