Skip to content

Commit

Permalink
favicon sizes fix
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Dec 7, 2023
1 parent 67b9501 commit e23fbdc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
15 changes: 7 additions & 8 deletions plugins/favicon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ export const defaults: Options = {
favicons: [
{
url: "/favicon.ico",
size: 32,
size: [16, 32],
rel: "icon",
format: "ico",
},
{
url: "/apple-touch-icon.png",
size: 180,
size: [180],
rel: "apple-touch-icon",
format: "png",
},
Expand All @@ -46,7 +46,7 @@ export const defaults: Options = {

export interface Favicon {
url: string;
size: number;
size: number[];
rel: string;
format: string;
}
Expand Down Expand Up @@ -128,7 +128,7 @@ export default function (userOptions?: Options) {
for (const favicon of options.favicons) {
addIcon(document, {
rel: favicon.rel,
sizes: `${favicon.size}x${favicon.size}`,
sizes: favicon.size.map((s) => `${s}x${s}`).join(" "),
href: site.url(favicon.url),
});
}
Expand All @@ -149,7 +149,7 @@ function addIcon(document: Document, attributes: Record<string, string>) {
async function buildIco(
content: Uint8Array,
format: keyof sharp.FormatEnum | "ico",
size: number,
size: number[],
cache?: Cache,
): Promise<Uint8Array> {
if (cache) {
Expand All @@ -166,12 +166,11 @@ async function buildIco(
const resizeOptions = { background: { r: 0, g: 0, b: 0, alpha: 0 } };
const img = sharp(content);
image = await sharpsToIco(
img.clone().resize(16, 16, resizeOptions),
img.clone().resize(32, 32, resizeOptions),
...size.map((size) => img.clone().resize(size, size, resizeOptions)),
);
} else {
image = await sharp(content)
.resize(size, size)
.resize(size[0], size[0])
.toFormat(format)
.toBuffer();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/__snapshots__/favicon.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ snapshot[`favicon plugin 3`] = `
<html><head>
<title>Hello world</title>
<link rel="icon" href="/favicon.svg" type="image/svg+xml">
<link rel="icon" sizes="32x32" href="/favicon.ico">
<link rel="icon" sizes="16x16 32x32" href="/favicon.ico">
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
</head>
<body>
Expand Down

0 comments on commit e23fbdc

Please sign in to comment.