From 5c3da58366b12282bdc34905369b867690658b94 Mon Sep 17 00:00:00 2001 From: SleeplessOne1917 <28871516+SleeplessOne1917@users.noreply.github.com> Date: Mon, 22 Jul 2024 14:19:11 +0000 Subject: [PATCH] Fix issue from logo bugfix (#2620) * Fix issue from previous bugfix * Fix typo --- src/server/utils/generate-manifest-json.ts | 41 ++++++++++++++-------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/src/server/utils/generate-manifest-json.ts b/src/server/utils/generate-manifest-json.ts index 969f3d4b..2bdf6724 100644 --- a/src/server/utils/generate-manifest-json.ts +++ b/src/server/utils/generate-manifest-json.ts @@ -6,36 +6,47 @@ type Icon = { sizes: string; src: string; type: string; purpose: string }; const iconSizes = [72, 96, 128, 144, 152, 192, 384, 512]; let icons: Icon[] | null = null; +function mapIcon(src: string, size: number): Icon { + return { + sizes: `${size}x${size}`, + type: "image/png", + src, + purpose: "any maskable", + }; +} + +function generateDefaultIcons() { + return iconSizes.map(size => + mapIcon(`${getStaticDir()}/assets/icons/icon-${size}x${size}.png`, size), + ); +} + export default async function (site: Site) { if (!icons) { try { const icon = site.icon ? await fetchIconPng(site.icon) : null; - icons = await Promise.all( - iconSizes.map(async size => { - let src = `${getStaticDir()}/assets/icons/icon-${size}x${size}.png`; - - if (icon) { + if (icon) { + icons = await Promise.all( + iconSizes.map(async size => { const sharp = (await import("sharp")).default; - src = `data:image/png:base64,${await sharp(icon) + const src = `data:image/png:base64,${await sharp(icon) .resize(size, size) .png() .toBuffer() .then(buf => buf.toString("base64"))}`; - } - return { - sizes: `${size}x${size}`, - type: "image/png", - src, - purpose: "any maskable", - }; - }), - ); + return mapIcon(src, size); + }), + ); + } else { + icons = generateDefaultIcons(); + } } catch (e) { console.log( `Failed to fetch site logo for manifest icon. Using default icon`, ); + icons = generateDefaultIcons(); } }