[fetch] Initializing context/content specific fetch defaults (#43)

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: https://github.com/slightlyoff/ServiceWorker/issues/279#issuecomment-89756466
* Initializing fetch settings via "as": https://github.com/w3c/preload/pull/17#issuecomment-90398834
* 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 https://github.com/whatwg/fetch/issues/37#issuecomment-89957188: 

```
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`, and `fetch-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?


---
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/fetch/issues/43

Received on Tuesday, 7 April 2015 18:10:31 UTC