Fix issue from logo bugfix (#2620)

* Fix issue from previous bugfix

* Fix typo
This commit is contained in:
SleeplessOne1917 2024-07-22 14:19:11 +00:00 committed by GitHub
parent 5d124a3e14
commit 5c3da58366
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -6,36 +6,47 @@ type Icon = { sizes: string; src: string; type: string; purpose: string };
const iconSizes = [72, 96, 128, 144, 152, 192, 384, 512]; const iconSizes = [72, 96, 128, 144, 152, 192, 384, 512];
let icons: Icon[] | null = null; 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) { export default async function (site: Site) {
if (!icons) { if (!icons) {
try { try {
const icon = site.icon ? await fetchIconPng(site.icon) : null; const icon = site.icon ? await fetchIconPng(site.icon) : null;
icons = await Promise.all( if (icon) {
iconSizes.map(async size => { icons = await Promise.all(
let src = `${getStaticDir()}/assets/icons/icon-${size}x${size}.png`; iconSizes.map(async size => {
if (icon) {
const sharp = (await import("sharp")).default; 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) .resize(size, size)
.png() .png()
.toBuffer() .toBuffer()
.then(buf => buf.toString("base64"))}`; .then(buf => buf.toString("base64"))}`;
}
return { return mapIcon(src, size);
sizes: `${size}x${size}`, }),
type: "image/png", );
src, } else {
purpose: "any maskable", icons = generateDefaultIcons();
}; }
}),
);
} catch (e) { } catch (e) {
console.log( console.log(
`Failed to fetch site logo for manifest icon. Using default icon`, `Failed to fetch site logo for manifest icon. Using default icon`,
); );
icons = generateDefaultIcons();
} }
} }