From f84c03bbb3d0abe0b16c0bd3d1ac67f8f30fc174 Mon Sep 17 00:00:00 2001 From: Juliet Date: Fri, 13 Mar 2026 00:36:12 +0100 Subject: [PATCH] fix favicon fetch order --- src/worker.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/worker.js b/src/worker.js index c12c34a..b065b4b 100644 --- a/src/worker.js +++ b/src/worker.js @@ -505,6 +505,7 @@ async function handleFavicon(searchParams) { if (pageRes.ok && (pageRes.headers.get("content-type") ?? "").includes("text/html")) { let bestHref = null; let bestPriority = -1; + let bestSize = 0; const rewriter = new HTMLRewriter().on("link", { element(el) { @@ -513,14 +514,21 @@ async function handleFavicon(searchParams) { const href = el.getAttribute("href"); if (!href) return; - // Prefer apple-touch-icon > icon with sizes > icon > shortcut icon + // Prefer icon with sizes > icon > apple-touch-icon > shortcut icon let priority = 0; - if (rel === "apple-touch-icon") priority = 3; - else if (rel === "icon" && el.getAttribute("sizes")) priority = 2; - else if (rel === "icon") priority = 1; + if (rel === "icon" && el.getAttribute("sizes")) priority = 3; + else if (rel === "icon") priority = 2; + else if (rel === "apple-touch-icon") priority = 1; - if (priority > bestPriority) { + const sizesAttr = el.getAttribute("sizes") ?? ""; + const size = Math.max(...sizesAttr.split(/\s+/).map((s) => parseInt(s) || 0), 0); + + if ( + priority > bestPriority || + (priority === bestPriority && size > bestSize && size <= 64) + ) { bestPriority = priority; + bestSize = size; bestHref = href; } }, -- 2.51.2