Re: [whatwg] Fetch Objects and scripts/stylesheets

On Jul 21, 2014 9:55 AM, "Ben Maurer" <ben.maurer@gmail.com> wrote:
>
> I was walking with Will about how the browser prioritizes the loading of
> scripts and stylesheets. An idea that came up in our conversation was
> allowing the user to directly access Fetch objects associated with scripts
> and stylesheets. Some examples of how I could see this working:
>
> (1) Allowing the user to specify parameters to Fetch. For example, a user
> could say:
>
> <script src="/my.js" params="{'headers':{'myheader':'value'}}"
> id="myscript" />
>
> This would allow the user to make any type of request customization that
> they could for a direct call to Fetch.

This would indeed be cool. Though one downside is that sending custom
headers cross-origin requires the use of CORS and preflight requests. This
isn't to say that we shouldn't do it, but it will expose the author to more
complexity.

> (2) Allowing the user to access the in progress fetch object. For example,
> a user could say:
>
> $("myscript").fetch.then(function(resp) { console.log(resp.status); })
>
> Over time, the fetch object might add new functionality that would be
> useful to the application developer. For example, I could imagine the
fetch
> object gaining a way to change HTTP/2 priorities mid-request. Developers
> could take advantage of these functions for browser-initiated fetches.

As long as we can do this without requiring that the UA holds on to the
unprocessed byte stream forever. I.e. after the UA has parsed the script,
both the undecided byte stream and the decoded text stream should be
possible to throw away.

> (3) Allowing the user to construct a script or stylesheet from a fetch.
> Complex sites often want precise control over when scripts and stylesheets
> are loaded and inserted into the DOM. For example, today inserting a
> stylesheet blocks the execution of inline script tags. Some sites may know
> that this dependency is not needed. If one could construct stylesheets and
> scripts directly from a fetch, the author could separate the act of
> fetching the stylesheet from inserting into the DOM:
>
> var myfetch = window.fetch(...);
> myfetch.then(function(resp) {
>   document.body.appendChild(resp.body.asStyleSheet());
> });
>
> By calling asStyleSheet, the user could preserve the devtools experience
> that exists today while still having complete control over the fetching of
> their content.

I like the general idea, but I don't really love the syntax.

/ Jonas

Received on Thursday, 24 July 2014 05:03:43 UTC