Skip to content

Build your personal knowledge base with Trilium Notes

fix(fonts): discard a font load that a newer one overtook

Elian Doran7b21adec5775
diff --git a/apps/client/src/services/custom_fonts.spec.ts b/apps/client/src/services/custom_fonts.spec.ts
index d3cc92da80..aaacbed3c5 100644
--- a/apps/client/src/services/custom_fonts.spec.ts
+++ b/apps/client/src/services/custom_fonts.spec.ts
@@ -118,6 +118,36 @@ describe("applyCustomFontsFromOptions", () => {
expect(registeredFaces.has(staleFace)).toBe(false);
});
+ it("keeps the newest file when the load it overtook finishes after it", async () => {
+ mocks.options = { mainFontFamily: "customFont:fontNoteG7" };
+ mocks.notes = { fontNoteG7: { blobId: "blobG1" } };
+
+ // Holds the first file open, so the load for the file replacing it can overtake it.
+ let releaseFirst = () => {};
+ const firstHeld = new Promise<void>((resolve) => {
+ releaseFirst = resolve;
+ });
+ fetchMock.mockImplementationOnce(async () => {
+ await firstHeld;
+ return new Response(new Uint8Array([ 0x00, 0x01, 0x00, 0x00 ]));
+ });
+
+ const overtaken = applyCustomFontsFromOptions();
+ mocks.notes = { fontNoteG7: { blobId: "blobG2" } };
+ await applyCustomFontsFromOptions();
+ const newestFace = [ ...registeredFaces ][0];
+
+ releaseFirst();
+ await overtaken;
+
+ // The face built from the file the note holds now, not the one it held when the slower load
+ // set out to fetch it. Compared by identity: every face here carries the same family, and
+ // the stub files are byte-identical, so a structural match would hold either way.
+ expect([ ...registeredFaces ]).toHaveLength(1);
+ expect([ ...registeredFaces ][0]).toBe(newestFace);
+ expect(families()).toEqual([ "trilium-font-fontNoteG7" ]);
+ });
+
it("carries on where the note is gone or its file will not load", async () => {
mocks.options = { mainFontFamily: "customFont:goneNote001" };
await expect(applyCustomFontsFromOptions()).resolves.toBeUndefined();
diff --git a/apps/client/src/services/custom_fonts.ts b/apps/client/src/services/custom_fonts.ts
index dd03fd73ae..0b73649bd8 100644
--- a/apps/client/src/services/custom_fonts.ts
+++ b/apps/client/src/services/custom_fonts.ts
@@ -18,6 +18,13 @@ interface RegisteredFont {
/** The faces registered for the fonts the options currently name, by the note each is stored in. */
const registeredFonts = new Map<string, RegisteredFont>();
+/**
+ * The file each note's in-flight load is fetching. Loads for one note are not ordered — a newer one
+ * can overtake an older — so this is what an older load reads to find that it has been superseded
+ * and must not install the file it went to fetch.
+ */
+const loadingFonts = new Map<string, string>();
+
/**
* Registers the user's own fonts that the font options name, so the families the fonts stylesheet
* declares (see `getFontCss`) resolve to the files those notes hold. Fonts no longer named by any
@@ -37,6 +44,13 @@ export async function applyCustomFontsFromOptions() {
}
}
+ // A load still in flight for a font no longer named has nothing left to install.
+ for (const noteId of loadingFonts.keys()) {
+ if (!wanted.has(noteId)) {
+ loadingFonts.delete(noteId);
+ }
+ }
+
await Promise.all([ ...wanted ].map(async (noteId) => {
const note = await froca.getNote(noteId);
if (!note) return;
@@ -46,6 +60,8 @@ export async function applyCustomFontsFromOptions() {
const blobId = note.blobId ?? "";
if (registeredFonts.get(noteId)?.blobId === blobId) return;
+ loadingFonts.set(noteId, blobId);
+
try {
const face = await registerFontNote(noteId, customFontFamily(noteId), blobId);
const current = registeredFonts.get(noteId);
@@ -56,12 +72,21 @@ export async function applyCustomFontsFromOptions() {
return;
}
+ // These bytes are no longer the ones to draw with: while they were being fetched, a load
+ // for a file that replaced this one started, or the font stopped being named at all.
+ // Installing them here would put the older file back over the newer.
+ if (loadingFonts.get(noteId) !== blobId) {
+ document.fonts.delete(face);
+ return;
+ }
+
// The face built from the file this note held before goes only now that its replacement
// is registered, so nothing renders in the fallback family in between.
if (current) {
document.fonts.delete(current.face);
}
registeredFonts.set(noteId, { face, blobId });
+ loadingFonts.delete(noteId);
} catch (e) {
logError(`Could not load the font stored in note '${noteId}': ${e}`);
}