简体   繁体   English

为什么我们在预加载字体文件时需要“crossorigin”属性?

[英]Why do we need the "crossorigin" attribute when preloading font files?

To correctly preload font files, we're told we always need the crossorigin attribute applied to our <link rel="preload" /> tags, eg:为了正确预加载字体文件,我们被告知我们总是需要将crossorigin属性应用于我们的<link rel="preload" />标签,例如:

<link rel="preload" href="fonts/comicsans.woff2" as="font" type="font/woff2" crossorigin>

Can anybody give me the true reason for this?谁能给我这个的真正原因? All I can find are links in MDN that just dictate this requirement, without going into any detail as to why:我能找到的只是 MDN 中的链接,这些链接只是规定了这个要求,而没有详细说明原因:

https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types/preload#cors-enabled_fetches https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types/preload#cors-enabled_fetches

When preloading resources that are fetched with CORS enabled (eg fetch(), XMLHttpRequest or fonts), special care needs to be taken to setting the crossorigin attribute on your element.在预加载启用 CORS 的资源时(例如 fetch()、XMLHttpRequest 或字体),需要特别注意设置元素的 crossorigin 属性。 The attribute needs to be set to match the resource's CORS and credentials mode, even when the fetch is not cross-origin .该属性需要设置为匹配资源的 CORS 和凭据模式,即使获取不是 cross-origin

As mentioned above, one interesting case where this applies is font files .如上所述,一个有趣的例子是字体文件 Because of various reasons , these have to be fetched using anonymous-mode CORS (see Font fetching requirements).由于各种原因这些必须使用匿名模式 CORS获取(请参阅字体获取要求)。

It seems counterintuitive to my understanding of CORS, and why it's necessary.这似乎违反了我对 CORS 的理解,以及为什么它是必要的。 I'm sure there's a good reason for this, I just can't find it.我敢肯定这是有充分理由的,我只是找不到。

Before anybody just links to the same docs I'm talking about, see below:在任何人链接到我正在谈论的相同文档之前,请参见下文:

https://drafts.csswg.org/css-fonts/#font-fetching-requirements https://drafts.csswg.org/css-fonts/#font-fetching-requirements

I'd really like an in depth answer from somebody who really knows why this is a requirement, and what it's purpose is, with some evidence (docs, etc) to back it up.我真的很想从真正知道为什么这是一项要求及其目的是什么的人那里得到深入的回答,并提供一些证据(文档等)来支持它。

The HTML attribute crossorigin defines how to handle crossorigin requests. HTML 属性crossorigin定义了如何处理跨域请求。 Setting the crossorigin attribute (equivalent to crossorigin="anonymous" ) will switch the request to a CORS request using the same-origin policy.设置crossorigin属性(相当于crossorigin="anonymous" )将使用same-origin策略将请求切换为 CORS 请求。 It is required on the rel="preload" as font requests require same-origin policy. rel="preload"需要它,因为字体请求需要same-origin策略。

The same-origin policy is required on almost all new resource types, such as fetch() , <script type="module"> or fonts.几乎所有新的资源类型都需要同源策略,例如fetch()<script type="module">或 fonts。 It doesn't apply to legacy resource types (images, scripts, css, video, audio) because of backwards-compatibility issues.由于向后兼容问题,它不适用于旧资源类型(图像、脚本、css、视频、音频)。 <link rel="preload"> is a special case because it is a modern feature which needs to support legacy resource types, such as preloading an image. <link rel="preload">是一种特殊情况,因为它是一项需要支持旧资源类型的现代功能,例如预加载图像。

the ideal is, from now on, you always SOR by default when you introduce a new type of linking.理想的情况是,从现在开始,当你引入一种新的链接类型时,你总是默认 SOR。 It's just the right thing to do, because it lets us avoid having to care about a whole annoying class of security issues.这是正确的做法,因为它让我们不必关心整个烦人的 class 安全问题。 Source 资源

This requirement was then added to the W3C draft for CSS fonts.然后将此要求添加到 CSS fonts 的 W3C 草案中。

For font loads, user agents must use the potentially CORS-enabled fetch method defined by the [FETCH] specification for URL's defined within @font-face rules.对于字体加载,用户代理必须使用 [FETCH] 规范为@font-face 规则中定义的 URL 定义的可能启用 CORS 的提取方法。 When fetching, user agents must use "Anonymous" mode, set the referrer source to the stylesheet's URL and set the origin to the URL of the containing document.获取时,用户代理必须使用“匿名”模式,将引用源设置为样式表的 URL 并将源设置为包含文档的 URL。 Source 资源

I have also come across repeated comments that it was requested by the font foundries to prevent font piracy, but I cannot substantiate that with evidence.我也遇到过反复评论说字体代工厂要求它防止字体盗版,但我无法用证据证实这一点。

Other related links:其他相关链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我们有可用的 java 文件时,为什么我们需要 JSP 文件? - Why do we need JSP files when we have .java files available? 渲染属性时为什么需要转义&lt;和&? - Why do I need to escape < and & when rendering an attribute? 为什么设置@font-face时需要设置font-weight? - Why do I need to set font-weight when setting @font-face? 了解 crossorigin = &quot;use credentials&quot; 属性在发送用户凭据时的值 - understanding the value of the crossorigin = "use credentials" attribute when it sends user credentials 为什么我们需要“a:link”? 为什么不只是“一个”? - Why do we need “a:link”? Why not just “a”? 既然已经声明了类,为什么还需要使用元素? - Why do we need to use element when we have already stated class? 在 HTML 中何时以及为什么需要使用按钮标签的 value 属性的困惑 - The confusion with when and why we need to use value attribute of button tag in HTML 我是否需要本地托管资源中的完整性和跨域属性? - Do I need Integrity and crossorigin attributes in Locally Hosted Resources? 跨域属性的目的……? - Purpose of the crossorigin attribute…? 简单问题:预加载图像时是否需要指定“图像”的宽度和高度? - Simple question: Do I need to specify width, height of “image” when preloading images?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM