Repository navigation
Description
When initiating a fetch request for a resource, the user agent initializes the fetch with content/context specific settings. For example, when fetching an <img>, the UA applies the appropriate image-src CSP checks; may advertise different set of request headers (e.g. advertise support for some image formats via Accept header, and may emit additional "hint" headers); may set different transport options, such as request priority, dependencies, etc.
How do we enable the same functionality with fetch() API? Without the above, in the worst case, an image resource fetch initiated via fetch() may violate CSP, will return an incorrect/suboptimal asset due to missing headers, and may be fetched with incorrect priority. Of course, images are just one example; same logic applies to all other content types. Related discussions:
- I want to make an image request: "I want to make an image request" w3c/ServiceWorker#279 (comment)
- Initializing fetch settings via "as": (WIP) Remove loadpolicy attribute w3c/preload#17 (comment)
- Initializing fetch settings via markup attribute: https://www.w3.org/Bugs/Public/show_bug.cgi?id=26533
Intuitively, the difference between using fetch() vs. say, and <img>, is that the latter knows its context and is thus able to initialize all the right checks + properties. Perhaps we can address this whole issue by providing "treat this fetch as if it was an 'image'" setting or signal? Handwaving...
fetch(url, {as: 'image', headers: {...}, otherOpt: {}, ...})
- Use
as(or whatever fits best) as an explicit context signal that initializes appropriate defaults? headersand other fetch options are then applied on top of initialized defaults as overrides- As a last step, UA initializes any missing / not specified headers? E.g. User-Agent, etc.
The mental model here is very similar to what @domenic proposed in #37 (comment):
fetchSettings = mergeMultimaps(asDefaults, optSpecifiedSettings, uaDefaultSettings);
Assuming above sounds plausible, we could then also enable this in other parts of the platform:
<img src="photo.jpg" fetch-settings="{...}"> -- implicit:as == image, andfetch-settingscan be used to override other UA + fetch defaults.<link rel=preload as="image" href="photo.jpg" fetch-settings="{...}">--asis declared explicitly, and options passed in through same mechanism.
WDYT, does it sound crazy?
Activity
annevk commentedon Apr 8, 2015
To be clear,
<img>andfetch()already share logic. They both invoke fetch. That algorithm takes care of HSTS, CSP, Mixed Content, Referrer Policy, Service Workers, etc. E.g.fetch()ends up asconnect-srcin CSP.The problematic case with allowing
fetch()to set the CSP policy to something other thanconnect-srcis that you could then usefetch()to bypass CSP. The only way to prevent that would be to taint the response somehow. So that if you dofetch(url, {context: "image"})theResponseobject can only be used by<img>andbackground-imageand such.About initializing defaults. We could do this in fetch, but are engines actually setting these defaults based on an enum in the network layer or are they set in the DOM layer based on the environment? I can see how we could take over handling
Acceptin fetch, but client hints seems trickier?(As for header management. The way it works is that feature-specific headers are set by those invoking fetch. So currently e.g. the specification for
<img>would have to setAcceptand any client hint headers. And fetch adds headers in the network layer for CORS, cookies, etc. and these are therefore not exposed to service workers.)igrigorik commentedon Apr 9, 2015
That makes sense. In the case of
rel=preloadyou'd get same enforcement when you try to use the preloaded response within<img>, etc.. just as we noted here: w3c/preload#17 (comment).Our goal here is to provide context to the engine where its currently missing, such that the engine can make more informed decisions. For example, when the engine sees an
<img>resource, it will initialize some common defaults (e.g. Accept headers), and it may also use some environment/runtime variables to adjust priority and other settings: lower priority for below the fold images, advertise some hints if those values are known (e.g. resource width), and so on.By contrast, if you try to
fetch()an image resource today, the engine is completely blind and can't do any of the above. So, exposing "as", which can communicate that this is an "image" resource is already a big step forward. The engine may not have as rich of a context to determine all the other plausible optimizations, but our goal here is not to provide feature parity... When you're usingfetch()you're opting into "manual control", which is why custom headers and other properties are important -- e.g. I want to override the UA defaults and use fetch to advertise own hints and other fetch settings.In short, I think "as" is sufficient as a bootstrapping mechanism to help the UA set some defaults, it doesn't need to provide the exact same behavior.
annevk commentedon Apr 13, 2015
You have to get more specific than "engine" and "some defaults". We actually have to define that in more detail than just a hint since it is all rather observable and user agents will be required to support the same functionality.
igrigorik commentedon Apr 13, 2015
Sure, let's make it concrete. The developer provides the destination context via "as" attribute, e.g..
or
The user agent needs to...
I know that in Blink we advertise different headers, and assign different priorities, based on type of resource being fetched. AFAIK, same applies to FF, WebKit, and IE.
@pmeenan @mcmanus @toddreifsteck any thoughts or comments on this one?
annevk commentedon Apr 14, 2015
What I'd like to do is that if we're certain that e.g.
Acceptis always set based on a request context, we specify that logic as part of Fetch. That way it is clear whenAcceptis set, whether service workers can observe it, and what kind of values it can have. (That would mean that<img>no longer needs to talk aboutAcceptbut can depend on the request context.)And then go through that for each desired feature so that it's clear what the realm of a request context (e.g. "image") is and what's in the realm of a feature (e.g.
<img>).In the case of
fetch(url, {as: "image"})we'd need to restrict theResponseso that it can only be used by<img>andbackground-image(otherwise CSP is broken). At this point, that would only make such an API useful in service workers (and even that is a bit of a stretch as service workers have their own CSP policy), but perhaps in the future<img>et al can be fed aResponsedirectly.igrigorik commentedon Apr 15, 2015
That makes sense. What's the best way to tackle this? Go through each context and compile a set of context-specific headers and values into a table, or some such? Would it make sense to draft some fetch-spec boilerplate that we can fill in as we go? Might help clarify what we need, etc.
Not sure I follow the "useful in SW only" - how so? For
<img>case, I'm picture: fetch() the data, thenimg.src = window.URL.createObjectURL(responseBlob)... not sure what the right/required security plumbing to make that work though.annevk commentedon Apr 15, 2015
So
AcceptandAccept-Languageare set by pretty much all contexts except for "fetch".EventSourcesets an header but it needs to set that itself. (Also, I'm not sure it makes sense to allowfetch(url, {as:"eventsource"}). Perhaps we should limit the allowed values initially.) I could come up with language forAcceptandAccept-LanguageI think.Re: "useful for SW only". There is no
Blobfor opaque responses. So we'd need some new API that creates a URL from aResponseto make that work in such a way. I was even thinking that it might be nice if that API could take a promise, so you could have:(The other reason why I think we want it for a
Responserather than a body of sorts is that then we keep the response headers available.)annevk commentedon Apr 15, 2015
An alternative approach would be to reuse the media element
srcObjectAPI on other objects and make one of the accepted objects aResponse. That does seem a little cleaner as using a URL effectively requires another roundtrip through Fetch (and gets a bit ickier with GC). @zcorpan, issrcObjectbeing considered for<img>?zcorpan commentedon Apr 15, 2015
Yes: https://www.w3.org/Bugs/Public/show_bug.cgi?id=23502 (marked as "Needs Impl Interest")
yoavweiss commentedon Apr 16, 2015
Having
asset context makes sense, but wouldn't that prevent authors from specifying various priorities to the fetched resource? e.g. If I have a background image that's extremely important and I want it to download in the same priority as CSS, I won't have a way to communicate that to the browser. IIRC that use case is the main reason the attribute is calledasrather thancontext.If we go that route then maybe, for the case of
<link rel=preload>, we need to add some priority hints on top of the context.annevk commentedon Apr 16, 2015
I don't understand why you think you would not be able to control the priority. That's a largely orthogonal feature we have not sorted out yet.
yoavweiss commentedon Apr 16, 2015
I agree that it's orthogonal to context, but the original intention of
aswas to indicate download priority to the browser. The way this discussion is going, we'll probably need to add another, separate hints to enable the "download a low priority context in high priority" use case. (which is fine, but we'd need to address that)annevk commentedon Apr 16, 2015
If the intent was fetch priority,
aswould be way too generic a name and not that useful (priority is much more than a context). I recommend opening up a distinct issue for indicating priority.igrigorik commentedon Apr 21, 2015
@annevk I'll defer to you on
createResponseURLvs.srcObject, you have much better context there.@yoavweiss I agree with @annevk I think changing priorities is an orthogonal discussion. "as" is used to initialize request defaults, other options provided by the developer would override them. For example...
aswould set the default image Accept headerChanging prioritization would work the same way, we "just" need to define syntax to control transport priorities and dependencies... previous discussion on whatwg. Assuming we arrive at some such thing, it could potentially look as... commence handwaving...
<link rel=preload href=photo.jpg as=image fetch-settings="{headers: {..}, priority: {..}}">Or some such.
On a more tactical level, I'm wondering if we can unblock Resource Hints / Preload and continue iterating on other details in parallel... Concretely:
asfetch option, with some starter language to indicate that it should be used to enforce security policies and initialize request defaults.The response blob vs. URL does not block either RH or Preload because both are used to populate the cache, and are then consumed by another element/object that would enforce its CSP policy.
@annevk wdyt, does that sound reasonable? I'd love to land the RH and Preload refactors, as they're blocking implementation in Blink.
annevk commentedon Apr 21, 2015
I think for Resource Hints / Preload
ascould setcontextdirectly, no? So maybe we should just call itcontext? I'm still a bit concerned though. E.g. ifscript-srcandimg-srcare equal something fetched as image could be used as script and the other way around. But maybe that is not different from today. Just wondering whether there is anyway in which this might cause us problems going forward.6 remaining items
mnot commentedon May 1, 2015
It'd be nice, but I suspect implementations are going to want to do their own thing here, to a degree.
Fetch could give advice about what's appropriate; e.g., sending image/wepb for a HTML fetch doesn't make sense (I get that they're trying to hint to the server that it's OK to send links to webp images in the HTML, but that's not really what accept is for, and other browsers don't do it).
We could also save a few bytes by recommending that they not send /; it doesn't do anything (especially when it's on its own)`.
Latest and greatest:
http://httpwg.github.io/specs/rfc7231.html#header.accept
igrigorik commentedon May 1, 2015
The problems are: inline images; direct image requests (when we don't have the
<img>context to set the appropriate header); hint to HTML/CSS/JS that WebP can be used. In practice, we need this -- see crbug.com/267212 for context. FWIW, latest IE is doing the same advertisement for jpegxr.Been tried, sadly omitting the Accept header breaks bunch of sites. I'll see if I can dig up the old thread around this.
P.S. That said.. some consistency would be nice. It would help weed out things like crbug.com/443094
annevk commentedon May 1, 2015
I think Fetch could have a "SHOULD" value for
Acceptheaders to encourage user agents to converge. But it's important to define this since the point where the UA adds theAcceptheader is going to be observable. E.g.Acceptwe want to set before service workers,Hostwe want to set after (when we go to the network). If we ignore interoperability on the value, that timing alone is somewhat crucial.Extract main fetch in preparation for setting request headers based o…
Set Accept/Accept-Language in Fetch per #43
annevk commentedon May 4, 2015
I have added header initialization for
AcceptandAccept-Language. What remains is opaque responses for when a request context is set by developers. And feeding responses to an API (either via URL or object). Should we create new issues for those? Anything remaining here?igrigorik commentedon May 4, 2015
@annevk nice. A couple of questions...
annevk commentedon May 5, 2015
Well, it already says "should" so user agents have some leeway and I think user agents ought to converge on supported types long term. I'd rather not mention formats that don't have buy in from everyone.
We can mention priorities I suppose. Pointer? Part of the problem with priorities is that I know that e.g. in Gecko we want to do more. We want to not only take into account the context, but also layout information and such. So setting priority in Fetch may be too late.
igrigorik commentedon May 5, 2015
Fair enough 👍
I don't think it's any different from Accept, which may also be set upstream - e.g. through explicit Accept header passed to fetch(). For priority, we could use similar logic: "if requests transport priority is not set, use the request's context to initialize the transport priority", or some such. Perhaps we can just add this as a substep in fetching algorithm - say, between current steps 2-3?
annevk commentedon May 6, 2015
That seems reasonable. What reference should I use for priorities?
igrigorik commentedon May 6, 2015
The best definition is "stream (request) priority" section in HTTP/2: https://tools.ietf.org/html/draft-ietf-httpbis-http2-17#section-5.3. For HTTP/1.x the priority is not communicated to the server, but is used internally by the net-stack to prioritize order and request dispatch times.
Perhaps... ~"if requests transport priority is not set, use the request's context to initialize the request priority - e.g. stream weight and dependency for HTTP/2, or equivalent priority class used to prioritize dispatch and processing of HTTP/1.x requests."
annevk commentedon May 6, 2015
I guess I should also give request a priority field then whose value and function is mostly left as an exercise to the reader.
igrigorik commentedon May 7, 2015
Yep, sounds good. As a side note, there was this: http://lists.w3.org/Archives/Public/public-whatwg-archive/2014Aug/0081.html .. except, we never reached any meaningful conclusions on that thread.
That said, now that the dust has settled on HTTP/2, I think we can take another run at it. In the meantime, exposing a "priority" field on fetch is sufficient. We can refine how that looks later.
Say something rather vague about priority to at least acknowledge it …