Skip to content

Commit

Permalink
Set sizes prop to any for svg icons (#52609)
Browse files Browse the repository at this point in the history
For `link` tag's `sizes` property, the property is defined as:

> This attribute defines the sizes of the icons for visual media contained in the resource. It must be present only if the [rel](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#rel) contains a value of icon or a non-standard type such as Apple's apple-touch-icon. It may have the following values:
`any`, meaning that the icon can be scaled to any size as it is in a vector format, like image/svg+xml.

x-ref: https://html.spec.whatwg.org/#attr-link-sizes
x-ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link (`sizes` property definition section) 

Closes #52002
Closes NEXT-133
  • Loading branch information
huozhi committed Jul 13, 2023
1 parent 4c4bee7 commit 76eec86
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
Expand Up @@ -26,9 +26,9 @@ interface Options {
async function nextMetadataImageLoader(this: any, content: Buffer) {
const options: Options = this.getOptions()
const { type, segment, pageExtensions, basePath } = options
const numericSizes = type === 'twitter' || type === 'openGraph'
const { resourcePath, rootContext: context } = this
const { name: fileNameBase, ext } = path.parse(resourcePath)
const useNumericSizes = type === 'twitter' || type === 'openGraph'

let extension = ext.slice(1)
if (extension === 'jpg') {
Expand Down Expand Up @@ -144,7 +144,7 @@ async function nextMetadataImageLoader(this: any, content: Buffer) {
}`
}

const imageSize = await getImageSize(
const imageSize: { width?: number; height?: number } = await getImageSize(
content,
extension as 'avif' | 'webp' | 'png' | 'jpeg'
).catch((err) => err)
Expand All @@ -159,11 +159,11 @@ async function nextMetadataImageLoader(this: any, content: Buffer) {
...(extension in imageExtMimeTypeMap && {
type: imageExtMimeTypeMap[extension as keyof typeof imageExtMimeTypeMap],
}),
...(numericSizes
? { width: imageSize.width as number, height: imageSize.height as number }
...(useNumericSizes && imageSize.width != null && imageSize.height != null
? imageSize
: {
sizes:
extension === 'ico'
extension === 'ico' || extension === 'svg'
? 'any'
: `${imageSize.width}x${imageSize.height}`,
}),
Expand Down
1 change: 1 addition & 0 deletions test/e2e/app-dir/metadata/metadata.test.ts
Expand Up @@ -646,6 +646,7 @@ createNextDescribe(

const iconSvg = $('link[rel="icon"][type="image/svg+xml"]')
expect(iconSvg.attr('href')).toBe('/icon.svg?90699bff34adba1f')
expect(iconSvg.attr('sizes')).toBe('any')

$ = await next.render$('/basic')
const icon = $('link[rel="icon"]')
Expand Down

0 comments on commit 76eec86

Please sign in to comment.