-
Notifications
You must be signed in to change notification settings - Fork 31
Load event insufficient #89
Comments
Can that markup also include |
Yes but that wouldn't really help if LABjs isn't present yet (which I can't guarantee). A related hack (hopefill/prollyfill) I'm presently experimenting with is to have an onload that sets the 'readyState' onto the element itself so LABjs can later inspect it. That's pretty ugly and it's really only something I'd want to do for real if it seemed the standard might actually do that eventually. |
You can use the Alternatively, you can use |
The other big problem with any onload solution is CSP. Feels like a deal breaker. |
True. No such problem with the |
Is there any CORS related issues to that if you're checking on URLs that are from a different domain or anything like that? |
Looks like this approach may work but is a bit brittle, in that |
|
A |
Sure, doable but tougher to get right if there's a |
All in all, these are still hacky workarounds for what I claim should be a supported use-case. If the browser engine can tell "immediately" if a resource is preloaded -- it obviously can because it fires a another load event if you create an extra |
This request is out of scope for preload - you could make a similar claim for e.g. If you feel the current solutions are not sufficient, I suggest you start up a thread on the WICG's discourse and see if others share your pain. |
I will happily start a thread in that wider discussion group. But I just have to say that I'm quite disappointed with the repeated and spurious claim of "out of scope". I believe it is 100% and entirely in scope, or I wouldn't have spent the time to bring it up here. When I see "out of scope", what I'm actually reading is, "a problem I don't want to deal with", not "a problem that doesn't apply to this area." That's reinforced by the fact that we have nearly a dozen messages in this thread where the focus was on trying to invent some other hack rather than addressing the merits of the use-case itself. Name any type of web content: images, stylesheets, video/audio, scripts, etc. In all of these cases, the use case of "resource preloading" applies. And in all of these cases, This isn't at all like For resource preloading however, the whole point is that you're trying to load it in a container that doesn't apply it to the page, and therefore there's no other way to tell if it loaded other than a robust feature built into the container itself. This is the primary distinguishing feature from The The reason it's not enough is because "best practice" is to preload via markup (early parser discovery) and then progressively enhance your resource loading with some script logic (like a resource loader). That creates the inherent race condition I complained about in my OP, between what the markup does and what the code can detect. If you're preloading something with markup, the code side needs to be able to fully determine the status of that mechanism. To suggest that this gap in preloading functionality should be handled by some other part of the web platform (or not at all) is just painfully myopic. |
The reason I claim this is out of scope is that such a mechanism, if indeed needed, should not apply only to preloaded resources. For example, we already discuss similar concepts that can answer the "was this resource loaded yet?" question as part of whatwg/fetch#65. In any case, I suggest you specify why your use-case is not addressed by something like
|
After re-reading my previous message, I apologize for my aggressive tone. That was not intentional. I've been strenuously advocating (on and off) for resource preloading for about 8 years now, with so many discussion threads and proposals and debates back and forth, I've lost count. I was thrilled when That's why it's so frustrating to be so close to having a workable system but be told that the last missing piece -- which is critical IMO -- is just "out of scope". Fundamentally, whether spec folks agree or not (for years there was disagreement even philosophically on this point), there will always be some of us out here that want full script-control over resource preloading, for a variety of use-cases that markup-alone will never solve well. |
Unless I've missed something and we're talking about a markup-oriented version of fetch, I think this is a very different kind of use-case. Script-controlled APIs (as opposed to reflective properties from DOM elements) have a whole world of different capabilities available to them to eliminate the race conditions. But markup-to-script bridges have this inherent race condition with no clear way to solve it. If there are other markup containers besides |
Take the design of the Promises API, for example. This mechanism does not allow an outside observer to tell whether or not the value has been resolved. But they solved the inherent race condition by saying that no matter when you register a So, you don't need a way to tell if the value is there, you just declaratively state what you want to do when it is there and leave the timing up to the system. The equivalent would be a They knew, exactly as I'm saying now, that designing a system with an inherent race condition in it is a bad idea. So they just eliminated the possibility. |
It is not the case that I'm suggesting there's no hacky workarounds that (at least partially) address my need. There clearly are multiple ways to hack at this, as this thread demonstrates. But this thread isn't like a support ticket asking "how do I do something?" I'm still trying to make the principled case that this missing functionality is a gap that should be addressed. So far, the only concrete reason I've been given for why it shouldn't be addressed here is that there may be other places in the web platform that would also like it addressed. It's not at all clear to me that a single system would serve all those use cases. But even if it was, this thread could (and should) be used to bolster support for that. Spec authors always want concrete use cases for features, and this is one such case. However, the tone here feels a bit more skeptical and hands-off'ish, with phrases like "if indeed needed". I have I believe demonstrated a concrete reason why the feature needs to exist, and so far haven't seen a concrete reason why it shouldn't exist. That's frustrating. |
Heck, for that matter, I know I've seen somewhere in spec discussions the idea of a But why wouldn't we take the opportunity to use this current concrete use case as the impetus to push for something, and use that as a proof-point for other potential consumers of such behavior? Why kick the can down the road to wait for somewhere else in the platform to finally address it? Why not let this be the test case to appeal to the supreme court, if you will? |
|
BTW, I made a testing resource related to https://discourse.wicg.io/t/mocking-resource-loading-behavior/2104 |
I brought up this topic in #10 and #20 and it was discussed. But I'm reopening the discussion because in my current use-case experience implementing a script loader, I'm finding the existing behavior is concretely insufficient.
The problem can be easiest illustrated with the inherent race condition of this code:
Now, that's not actually what I'm doing in code. But that race condition is deeply concerning. I know there's lots of places that such a race condition exists, but I believe this use-case I'm about to describe makes the pain acute enough that it should be revisited for some sort of special handling.
What I'm doing with my script loader (3.0 rewrite of LABjs) is to have it create and inject
<link rel=preload>
elements, listen to theirload
events, then re-request then with<script async>
tags in desired order of execution. That part works fine with behavior as-is.But where it falls apart is the desire, pushed especially by the recent pressure on web perf for PWAs, etc, to push as much of resource loading to declarative markup as possible.
I have a server-side component for LABjs which analyzes your resource tree to determine what the client will need to load and in what order. This tool can produce the client code to load these resources with the
$LAB
API fine. But it also produces the markup of the<link rel=preload ..>
tags, for insertion into the initial HTML response.This is considered "best practice" from a web perf perspective, to get critical resources loading as possible.
Unfortunately, we now have a race condition. If LABjs finds the
<link rel=preload>
element in the DOM, and subscribes a load event, I can't guarantee if I'm going to get that event or if it already passed. That's clearly broken.The only option then is for LABjs to create and insert another
<link rel=preload>
element so that I can hear the event (which if it's already loaded, will essentially fire "on the next tick").That's deeply troubling to have to churn DOM modifications unnecessarily just to get assurances of an event notification. It violates another set of performance best practices to force that extraneous DOM modification.
The unenviable position LABjs finds itself in is: either suck for modifying the DOM unnecessarily, or suck for not getting resources markup-discoverable early enough.
Presumably the engine has some deterministic way to ask if the resource is "already loaded" and not actually make a second request. It just finds that it's already in some-whatever-cache and then fires the load event (async).
This use case shows that we really need to expose some mechanism by which the code can determine if the thing is already loaded (ie, the load event has already passed), to avoid needing to mutate the DOM to figure that out.
The deterministic logic would then be (something like):
I don't care what the property or mechanism for determination is (but simplest is best). It could be something like the above (the
document.readyState
property would be precedent). Or it could be like "always fire the load event if if the element has already finished loading" (precedent is jQuery's ubiquitous.ready(..)
event handling).I just don't want it to involve having to mutate the DOM or make unnecessary network requests to figure it out. Doing so is performance counter-productive, for a feature that is explicitly performance focused.
In light of this use-case, can we please revisit how to determine if a resource is preloaded?
The text was updated successfully, but these errors were encountered: