大网数据广告活动
网站运营与故障问答

When should I use the “crossorigin” attribute on a “preconnect” <link>?

· 52CE 我爱测

I’d like to include some preconnect resource hints on my site so that browsers can (for example) connect to the jQuery CDN before they actually see the script tag that invokes the CDN. I’m not sure whether I should include the “c…

问题原文

原帖链接 · bdesham · CC BY-SA 3.0

I’d like to include some preconnect resource hints on my site so that browsers can (for example) connect to the jQuery CDN before they actually see the script tag that invokes the CDN. I’m not sure whether I should include the “crossorigin” attribute or what its value should be. The spec says, in part,

To initiate a preconnect, the user agent must run these steps:

[…]

  1. Let corsAttributeState be the current state of the element's crossorigin content attribute.
  2. Let credentials be a boolean value set to true.
  3. If corsAttributeState is Anonymous and origin is not equal to current Document's origin, set credentials to false.
  4. Attempt to obtain connection with origin and credentials.

I don’t know how to interpret this algorithm. If I’m preconnecting to a CDN, which will let anyone download its content without any sort of credentials, what value should I use for the “crossorigin” attribute?

已采纳解答

原帖链接 · jakub.g · CC BY-SA 4.0

All the answers so far seem either simplified, incomplete or partially wrong (topic is complex, things are confusingly named and not well documented!), so here's my understanding:

To be able to reuse the connection created by <link rel=preconnect>, things depend on what kind of content you want to fetch, from where, whether the request will send browser credentials (which can be established by the browser explicitly or implicitly):

The request is same-origin (example.com requests subresource from example.com)

There's no need for preconnect at all in the first place; the browser keeps the connection open after loading the page for quite a while. If there are multiple connections to be opened, the browser decides by itself if and how many to open (depending if server announces HTTP/2 support in TLS handshake, browser settings etc.)

to be checked: what if same-origin request has crossorigin attribute: is it used or ignored?

The request is cross-origin (example.com requests subresource from another.com)

  • if the actual request has crossorigin attribute explicitly set in HTML (crossOrigin in JS - case is important), the preconnect must also have it, with same value (perhaps except in cases where it doesn't make sense and crossorigin is ignored -- not fully clear for me yet)
  • else, if request if for <script type=module>: to be checked
  • else, if request is and "old school" request for <img>, <style type=stylesheet>, <iframe>, classic <script> etc. (initiated via HTML or JS) without crossorigin explicitly specified, then the preconnect MUST NOT have crossorigin attribute set.
  • else, if request is a cross-origin font request, preconnect must have crossorigin=anonymous
  • else, if request is a cross-origin fetch or XHR:
    • if it is done in credentialed mode (i.e. cookies are attached or HTTP basic auth is used; in case of fetch, this means credentials !== omit; in case of XHR: withCredentials === true): preconnect must have crossorigin=use-credentials
    • if it is not in credentialed mode: preconnect must have crossorigin=anonymous

For the last case (fetch/XHR), go to network panel in Chrome/Firefox devtools, right click a request, and choose copy as fetch from a dropdown. This will create a snippet of JS, which will tell you if that request is CORS-enabled ("mode"=="cors") and credentialed ("credentials"=="include"|"same-origin").

Note however the trick above doesn't work correctly for non-XHR/fetch requests, because for example fetch and <img> use different algorithms to establish connection, as explained before.

Finally, in HTML, <link ...crossorigin> === <link ...crossorigin=anonymous>.

Additional notes and links:

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2016-02-01)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。