You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 5, 2022. It is now read-only.
Repository navigation
This repository was archived by the owner on May 5, 2022. It is now read-only.
Intuitively, preload is a declarative fetch()... so, "connect-src" as default seems reasonable, since that's what fetch() is subject to [3] -- any objections?
The fact that preload is declarative, and the fact that a script cannot observe the response of the declarative preload, are exactly what make connect-src wrong. connect-src would only be correct if the preload is for a resource that's going to be fetched with fetch() or XHR. But, it seems unclear (probably unlikely, IMO) that most preloads without an "as" are really going to used for fetch() or XHR.
Is it really important to have a default context? If there's no default context, but instead the UA just logs a warning to the console saying the prefetch will be ignored due to a missing "as" attribute, then the developer is likely to notice the message and/or the fact that the prefetch doesn't happen, and add the (probably) correct "as" attribute.
I'm a fan of having defaults for making the developer's life easy when the default is intuitive, but there doesn't seem to be any intuitive default here. Having the developer explicitly tell us the correct context is likely going to help us with prioritization and maybe withapplying the correct referrer policy and other security things. Assuming the wrong context means we'll get those things wrong. So, I lean towards the behavior I described in the previous paragraph.
I think a lot of developers would react to this message by adding "connect-src cdn.example.com" to their CSP. But, that is not the best thing to do. Conversely, if the message was:
Given that the primary purpose of "as" is to initialize the right request priority and headers (to minimize unnecessary contention), the mandatory requirement might actually be a good one in that it forces the developer to explicitly think through and declare how this resource should be treated. A clean(er) integration with CSP is also a nice bonus.
If I preload as="script" but I then load the URL with some other container (iframe, etc) or I just Ajax it, does that imply any kind of problem or failure?
If I preload as="script" but I then load the URL with some other container (iframe, etc) or I just Ajax it, does that imply any kind of problem or failure?
No. "as" is used as a signal to initialize fetch settings: priority, settings, etc. As such, you can "lie" and fetch an asset with a different priority (that's a feature in some cases), but in practice this should be used very carefully (generally, it's a bad idea), since it does run the risk of sending out the wrong headers and/or affecting how the resource is cached.
If I preload as="script" but I then load the URL with some other container (iframe, etc) or I just Ajax it, does that imply any kind of problem or failure?
This actually would result in a double download, since the browser (at least in the Blink implementation) uses the as value to determine the resource type, and only resources from the same types can be reused, unless we somehow carve out preload from that match check.
Activity
igrigorik commentedon Jan 27, 2015
Given that the primary purpose of "as" is to initialize the right request priority and headers (to minimize unnecessary contention), the mandatory requirement might actually be a good one in that it forces the developer to explicitly think through and declare how this resource should be treated. A clean(er) integration with CSP is also a nice bonus.
Any objections?
getify commentedon Mar 10, 2015
Another thought related to
as=..:If I preload as="script" but I then load the URL with some other container (iframe, etc) or I just Ajax it, does that imply any kind of problem or failure?
getify commentedon Mar 10, 2015
Also possibly related to #14
igrigorik commentedon Mar 11, 2015
No. "as" is used as a signal to initialize fetch settings: priority, settings, etc. As such, you can "lie" and fetch an asset with a different priority (that's a feature in some cases), but in practice this should be used very carefully (generally, it's a bad idea), since it does run the risk of sending out the wrong headers and/or affecting how the resource is cached.
make 'as' attribute mandatory
yoavweiss commentedon Feb 16, 2016
This actually would result in a double download, since the browser (at least in the Blink implementation) uses the
asvalue to determine the resource type, and only resources from the same types can be reused, unless we somehow carve out preload from that match check.