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? headers
and 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-settings
can be used to override other UA + fetch defaults.<link rel=preload as="image" href="photo.jpg" fetch-settings="{...}">
--as
is declared explicitly, and options passed in through same mechanism.
WDYT, does it sound crazy?