Skip to content

Build your personal knowledge base with Trilium Notes

fix(link previews): keep an icon at the size a high-resolution screen needs

Elian Doranb38f7a829fb0
diff --git a/apps/server/src/routes/api/link_embed.spec.ts b/apps/server/src/routes/api/link_embed.spec.ts
index 0f4cfd3348..d4449d9c4f 100644
--- a/apps/server/src/routes/api/link_embed.spec.ts
+++ b/apps/server/src/routes/api/link_embed.spec.ts
@@ -476,15 +476,17 @@ describe("link-embed getMetadata", () => {
expect(result.favicon).toBeUndefined();
});
- it("keeps only the size of an icon a preview draws, so a large one is not lost to the ceiling", async () => {
- // The shape that made triliumnotes.org show no icon at all: 114KB across six sizes, of
- // which the rendered entry is 1.1KB. Fetched whole and stored trimmed, because the sizes
- // are what the choice is made from — enforcing the ceiling on the download instead simply
- // threw the icon away.
+ it("keeps one size out of an icon offering several, so a large file is not lost to the ceiling", async () => {
+ // The exact shape that made triliumnotes.org show no icon at all: six sizes totalling
+ // 114KB, over the ceiling a favicon is allowed. Fetched whole and stored trimmed, because
+ // the sizes are what the choice is made from — enforcing the ceiling on the download
+ // instead aborted the stream and threw the icon away.
const html = `<html><head><title>Plain</title><link rel="icon" href="/fav.ico"></head></html>`;
const fat = makeMultiSizeIco([
{ edge: 16, bytes: 1128 },
{ edge: 32, bytes: 4264 },
+ { edge: 48, bytes: 9640 },
+ { edge: 64, bytes: 16936 },
{ edge: 128, bytes: 67624 },
{ edge: 256, bytes: 14550 }
]);
@@ -499,8 +501,9 @@ describe("link-embed getMetadata", () => {
expect(result.favicon).toBeTruthy();
expect(stored("favicon")?.fileName).toBe("example.com.ico");
- // The 16x16 entry, and a one-entry directory to hold it.
- expect(stored("favicon")?.buffer.byteLength).toBe(22 + 1128);
+ // The 48x48 entry and a one-entry directory to hold it — not the 16x16 one, which is what
+ // a preview draws at and therefore exactly what a 2x or 3x display has to upscale.
+ expect(stored("favicon")?.buffer.byteLength).toBe(22 + 9640);
});
it("drops an icon that is one picture too large to keep, having nothing to give up", async () => {
diff --git a/apps/server/src/routes/api/link_embed.ts b/apps/server/src/routes/api/link_embed.ts
index 377f13a071..c0ad365d28 100644
--- a/apps/server/src/routes/api/link_embed.ts
+++ b/apps/server/src/routes/api/link_embed.ts
@@ -29,6 +29,18 @@ const MAX_RESPONSE_SIZE = 512 * 1024; // 512KB
const MAX_FAVICON_DOWNLOAD_SIZE = 256 * 1024; // 256KB
/** How large a favicon may be once trimmed. Icons that are one picture already measure a few KB. */
const MAX_FAVICON_SIZE = 64 * 1024; // 64KB
+/**
+ * The size of icon to keep out of a file offering several, in pixels.
+ *
+ * A preview draws its icon at 16 CSS pixels, so 16 is only enough for a display that has one device
+ * pixel per CSS pixel. Everything above that upscales it, visibly so on the 2x and 3x screens that
+ * are now ordinary — and the note is one document shown on whichever screen opens it, so the size
+ * cannot be chosen per viewer. 48 covers up to 3x and downscales cleanly below it, at a few KB
+ * against an attachment already kept once per site rather than once per link.
+ *
+ * A file that offers nothing this large gives its largest, which is the best it has.
+ */
+const FAVICON_TARGET_EDGE = 48;
/** Longest edge of the preview image kept in the note; larger images are scaled down to fit. */
const IMAGE_MAX_DIMENSION = 256;
@@ -217,7 +229,7 @@ async function downloadFavicon(faviconUrl: string): Promise<DownloadedPicture |
}
// An icon directory gives up the sizes nothing will look at; anything else is kept as it came.
- const trimmed = trimIcoToSmallestEntry(downloaded.buffer);
+ const trimmed = trimIcoToSmallestEntry(downloaded.buffer, FAVICON_TARGET_EDGE);
const bytes = trimmed ? Buffer.from(trimmed) : downloaded.buffer;
// The ceiling is on what is kept, not on what arrived: a file that is one large picture has
diff --git a/apps/server/src/services/ico.spec.ts b/apps/server/src/services/ico.spec.ts
index af542d73e7..bba6ccf084 100644
--- a/apps/server/src/services/ico.spec.ts
+++ b/apps/server/src/services/ico.spec.ts
@@ -113,6 +113,17 @@ describe("trimIcoToSmallestEntry", () => {
expect(readTrimmed(trimmed ?? new Uint8Array()).picture).toStrictEqual(payload(0xaa));
});
+ it("takes the floor it is given, a preview wanting more pixels than it draws", () => {
+ // Drawn at 16 CSS pixels, so 16 is what a 2x display upscales. The caller asks for the size
+ // that covers the screens it might be opened on, not the size it lays out at.
+ const sizes = [ 16, 32, 48, 64, 128 ].map((edge) => ({ edge, payload: payload(edge) }));
+
+ expect(readTrimmed(trimIcoToSmallestEntry(buildIco(sizes)) ?? new Uint8Array()).edge).toBe(16);
+ expect(readTrimmed(trimIcoToSmallestEntry(buildIco(sizes), 48) ?? new Uint8Array()).edge).toBe(48);
+ // Nothing that large on offer: the largest there is, rather than nothing.
+ expect(readTrimmed(trimIcoToSmallestEntry(buildIco(sizes), 512) ?? new Uint8Array()).edge).toBe(128);
+ });
+
it("settles for the largest when every picture is below the floor", () => {
const trimmed = trimIcoToSmallestEntry(buildIco([
{ edge: 8, payload: payload(0xcc) },