引用网站图标时,MIME 类型声明(使用类型属性)是强制性的吗?

When referencing favicons, is MIME-type declaration (using the type attribute) obligatory?

在 HTML 文档的 <head> 中引用 <link rel="icon"> 时,是否必须声明图标的 MIME 类型 使用属性 type="[MIME TYPE HERE]"?

的图像

或者 type 属性是可选的吗?

例如

是否必须写:

<link rel="icon" href="/path/to/my/favicon.png" sizes="57x57" type="image/png">
<link rel="icon" href="/path/to/my/favicon.svg" type="image/svg+xml">

或者仍然有效的写法:

<link rel="icon" href="/path/to/my/favicon.png" sizes="57x57">
<link rel="icon" href="/path/to/my/favicon.svg">

我问这个问题的原因:

今天,关于网站图标的主题,我读到:

The main favicon can be an SVG of any size. The type type="image/svg+xml" is unnecessary.

Source: https://medium.com/swlh/are-you-using-svg-favicons-yet-a-guide-for-modern-browsers-836a6aace3df

我对省略 type 并不感到完全不自在,但如果我可以从官方来源确认它绝对 没有必要 那么我愿意尝试习惯以后不包括它。

TLDR: 我的初步结论是 type 属性在 <link rel="icon" /> 中不是必需的,但它仍然是一个好主意包含它.


Mozilla 开发者网络 (MDN) 指出:

If there are multiple <link rel="icon">s, the browser uses their media, type, and sizes attributes to select the most appropriate icon.

Source: https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types

Web 超文本应用技术工作组WHAT-WG)指出:

If multiple icons are provided, the user agent must select the most appropriate icon according to the type, media, and sizes attributes [...] There is no default type for resources given by the icon keyword. However, for the purposes of determining the type of the resource, user agents must expect the resource to be an image.

Source: https://html.spec.whatwg.org/multipage/links.html#rel-icon

WHAT-WG 还指出:

If the type attribute is present, then the user agent must assume that the resource is of the given type [...] If the attribute is omitted, but the external resource link type has a default type defined, then the user agent must assume that the resource is of that type [...] If the attribute is omitted, and the external resource link type does not have a default type defined, but the user agent would fetch and process the linked resource if the type was known and supported, then the user agent should fetch and process the linked resource under the assumption that it will be supported.

User agents must not consider the type attribute authoritative — upon fetching the resource, user agents must not use the type attribute to determine its actual type. Only the actual type [...] is used to determine whether to apply the resource, not the aforementioned assumed type.

Source: https://html.spec.whatwg.org/multipage/semantics.html#concept-link-type-sniffing

所有这些对我来说意味着,始终包含 <link rel="icon">type 属性可能仍然是一个好主意 - 即使它不是严格意义上的规范要求。

whatwg.org 网站本身并未为其 SVG 徽标使用 type 属性,因此我认为可以肯定地说没有必要。

https://html.spec.whatwg.org/multipage/semantics.html#attr-link-type

The type attribute gives the MIME type of the linked resource. It is purely advisory. The value must be a valid MIME type string.

For external resource links, the type attribute is used as a hint to user agents so that they can avoid fetching resources they do not support.