问题原文
原帖链接 · 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:
[…]
- Let corsAttributeState be the current state of the element's
crossorigincontent attribute.- Let credentials be a boolean value set to
true.- If corsAttributeState is
Anonymousand origin is not equal to current Document's origin, set credentials tofalse.- 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
crossoriginattribute explicitly set in HTML (crossOriginin JS - case is important), the preconnect must also have it, with same value (perhaps except in cases where it doesn't make sense andcrossoriginis 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) withoutcrossoriginexplicitly specified, then the preconnect MUST NOT havecrossoriginattribute set. - else, if request is a cross-origin font request, preconnect must have
crossorigin=anonymous - else, if request is a cross-origin
fetchorXHR:- 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 havecrossorigin=use-credentials - if it is not in credentialed mode: preconnect must have
crossorigin=anonymous
- if it is done in credentialed mode (i.e. cookies are attached or HTTP basic auth is used; in case of fetch, this means
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:
- in Firefox, preconnect does not work even if it's supposed to
- three ways to check if preconnect is working
- browsers have some limits in how many parallel DNS requests can happen
- experimenting with preconnect with custom script injection on WebPageTest
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
