Skip to content

Commit 40b0c40

Browse files
fix(web): cache a font verdict only once every family in the list resolves
1 parent 8a57cd2 commit 40b0c40

2 files changed

Lines changed: 18 additions & 13 deletions

File tree

‎apps/web/src/appearanceFonts.test.ts‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe("createCachedFamilyProbe", () => {
5656
expect(probed).toEqual(['"Fira Code"']);
5757
});
5858

59-
it("keeps a pass only once the family resolves", () => {
59+
it("keeps a verdict only once the family resolves", () => {
6060
let resolved = false;
6161
let monospace = true;
6262
let probes = 0;
@@ -79,18 +79,22 @@ describe("createCachedFamilyProbe", () => {
7979
expect(probes).toBe(3);
8080
});
8181

82-
it("keeps a failure without waiting for resolution", () => {
82+
it("waits for every family in a list, not just a resolved fallback", () => {
8383
let probes = 0;
8484
const probe = createCachedFamilyProbe(
8585
() => {
8686
probes += 1;
8787
return false;
8888
},
89-
() => false,
89+
(family) => family === "Menlo",
9090
);
91-
expect(probe("Comic Sans MS")).toBe(false);
92-
expect(probe("Comic Sans MS")).toBe(false);
93-
expect(probes).toBe(1);
91+
// Menlo resolves, but the verdict came from it standing in for Late Mono.
92+
expect(probe("Late Mono, Menlo")).toBe(false);
93+
expect(probe("Late Mono, Menlo")).toBe(false);
94+
expect(probes).toBe(2);
95+
expect(probe("Menlo")).toBe(false);
96+
expect(probe("Menlo")).toBe(false);
97+
expect(probes).toBe(3);
9498
});
9599

96100
it("accepts empty input without probing", () => {

‎apps/web/src/appearanceFonts.ts‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,15 @@ export function areFontAdvancesMonospace(advances: readonly number[]): boolean {
206206
/**
207207
* Probing a family is up to 32 measureText calls, and setting the canvas font
208208
* to a new family string forces engine font matching on top, so verdicts are
209-
* kept keyed by the normalized family list. A failure always names a resolved
210-
* face and is final. A pass may be the generic fallback speaking for a face
211-
* that has not loaded yet, so it is only kept once the family resolves;
212-
* until then every call probes again, which is what lets the terminal
213-
* revalidate after a late face loads and still reject a proportional one.
209+
* kept keyed by the normalized family list. Until every family in the list
210+
* resolves, the verdict may be a fallback speaking for a face that has not
211+
* loaded yet, so it is not kept and each call probes again. That is what lets
212+
* the terminal revalidate after a late face loads and still reject a
213+
* proportional one.
214214
*/
215215
export function createCachedFamilyProbe(
216216
probe: (families: string) => boolean,
217-
resolves: (families: string) => boolean,
217+
resolves: (family: string) => boolean,
218218
): (family: string) => boolean {
219219
const verdicts = new Map<string, boolean>();
220220
return (family) => {
@@ -223,7 +223,8 @@ export function createCachedFamilyProbe(
223223
const cached = verdicts.get(families);
224224
if (cached !== undefined) return cached;
225225
const verdict = probe(families);
226-
if (!verdict || resolves(families)) verdicts.set(families, verdict);
226+
// Names never contain a comma (the input was split on it), so this is exact.
227+
if (families.split(", ").every(resolves)) verdicts.set(families, verdict);
227228
return verdict;
228229
};
229230
}

0 commit comments

Comments
 (0)