Unverified Commit de873d02 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Prefer svg icon instead favicon if possible (#53343)

### What?

Change the `favicon.ico` metadata `sizes` property from always `"any"`
to using the actual size possible

### Why?

When chrome/firefox browsers search for icon it needs the proper
metadata to determine which one to use, giving `favicon.ico` sizes with
`"any"` will prevent it loading the `icon.svg` as default icon when
available.

Changing to set proper size of `favicon.ico` (as the `favicon.ico` sizes
could be 16x16, 32x32, etc.) fixes the issue.

It works (loading svg icon) for chrome and firefox:

Firefox
<img width="505" alt="image"
src="https://github.com/vercel/next.js/assets/4800338/6873e595-479d-4d9e-ae5c-39e5f938fcf5">

Chrome
<img width="460" alt="image"
src="https://github.com/vercel/next.js/assets/4800338/03bbe4c7-ef76-4f34-a611-337b0d4b97a3">

It loads favicon.ico for Safari:
Using the `test/e2e/app-dir/metadata` app it shows the favicon.ico for
Safari
<img width="1000" alt="image"
src="https://github.com/vercel/next.js/assets/4800338/92cc8714-ea5e-432d-8144-2a4a42ee4ce2">

Can't have it as an e2e test as I tested it always loads the
`favicon.ico` in headless mode which can't determine the behaviors

Fixes #52002
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 0c70b9ff
......@@ -163,9 +163,14 @@ async function nextMetadataImageLoader(this: any, content: Buffer) {
? imageSize
: {
sizes:
extension === 'ico' || extension === 'svg'
? 'any'
: `${imageSize.width}x${imageSize.height}`,
// For SVGs, skip sizes and use "any" to let it scale automatically based on viewport,
// For the images doesn't provide the size properly, use "any" as well.
// If the size is presented, use the actual size for the image.
extension !== 'svg' &&
imageSize.width != null &&
imageSize.height != null
? `${imageSize.width}x${imageSize.height}`
: 'any',
}),
}
if (type === 'openGraph' || type === 'twitter') {
......
import type { BrowserInterface } from 'test/lib/browsers/base'
import { createNextDescribe } from 'e2e-utils'
import { check } from 'next-test-utils'
import { BrowserInterface } from 'test/lib/browsers/base'
import fs from 'fs/promises'
import path from 'path'
import cheerio from 'cheerio'
......@@ -669,7 +669,7 @@ createNextDescribe(
const favIcon = $('link[rel="icon"]')
expect(favIcon.attr('href')).toBe('/favicon.ico')
expect(favIcon.attr('type')).toBe('image/x-icon')
expect(favIcon.attr('sizes')).toBe('any')
expect(favIcon.attr('sizes')).toBe('16x16')
const iconSvg = $('link[rel="icon"][type="image/svg+xml"]')
expect(iconSvg.attr('href')).toBe('/icon.svg?90699bff34adba1f')
......@@ -678,7 +678,7 @@ createNextDescribe(
$ = await next.render$('/basic')
const icon = $('link[rel="icon"]')
expect(icon.attr('href')).toBe('/favicon.ico')
expect(icon.attr('sizes')).toBe('any')
expect(icon.attr('sizes')).toBe('16x16')
if (!isNextDeploy) {
const faviconFileBuffer = await fs.readFile(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment