mirror of
https://github.com/LemmyNet/lemmy-ui.git
synced 2024-11-01 01:59:56 +00:00
Fix issue from logo bugfix (#2620)
* Fix issue from previous bugfix * Fix typo
This commit is contained in:
parent
5d124a3e14
commit
5c3da58366
1 changed files with 26 additions and 15 deletions
|
@ -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;
|
||||||
|
|
||||||
export default async function (site: Site) {
|
function mapIcon(src: string, size: number): Icon {
|
||||||
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) {
|
|
||||||
const sharp = (await import("sharp")).default;
|
|
||||||
src = `data:image/png:base64,${await sharp(icon)
|
|
||||||
.resize(size, size)
|
|
||||||
.png()
|
|
||||||
.toBuffer()
|
|
||||||
.then(buf => buf.toString("base64"))}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
sizes: `${size}x${size}`,
|
sizes: `${size}x${size}`,
|
||||||
type: "image/png",
|
type: "image/png",
|
||||||
src,
|
src,
|
||||||
purpose: "any maskable",
|
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;
|
||||||
|
|
||||||
|
if (icon) {
|
||||||
|
icons = await Promise.all(
|
||||||
|
iconSizes.map(async size => {
|
||||||
|
const sharp = (await import("sharp")).default;
|
||||||
|
const src = `data:image/png:base64,${await sharp(icon)
|
||||||
|
.resize(size, size)
|
||||||
|
.png()
|
||||||
|
.toBuffer()
|
||||||
|
.then(buf => buf.toString("base64"))}`;
|
||||||
|
|
||||||
|
return mapIcon(src, size);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue