-
Notifications
You must be signed in to change notification settings - Fork 31
how to feature detect? #7
Comments
Good question, not sure. Are there mechanisms to test if browser implements particular relation type? E.g. prerender, etc? |
I have never considered FT for So, if a technique doesn't exist, I'd say it's a must that we invent something. |
I'm open to ideas :) /cc @marcoscaceres @slightlyoff |
Here's a thought: var s = document.create("link");
s.rel = "preload";
var supports = ("preload" in s); That is, it adds the |
An alternate is that instead of an var supports = ("onpreload" in document.create("link")); Side note: I like this option better semantically anyway, as "load" on a preloading-only element feels awkward. |
The more I think about this, the more I'm wary of trying to invent something in a context of this particular spec... This feels like a larger missing feature in the platform? Specifically, while we have libraries like Modernizr doing all kinds of crazy tricks in the background to feature detect particular features.. perhaps there should be a dedicated API to answer such things. |
Yeah, that's been needed for years. I've asked for (soft proposed) it several times in fact. There's a whole slew of things that are currently not feature detectable, that such an API could help with. Problem is people always say that the scope is too large to tackle it paired with whatever the one feature case is at the moment, so another ad hoc FT is designed, or (worse) FT is skipped (thankfully more rare these days). If you think you could push a Side note: this is somewhat tangential to my other feeling, which is that "client hints" should be extended to be able to send all the same kinds of info that a theoretical FT API would. |
The problem with this kind of API is that it's extremely problematic - what "supported" means is nebulous. See, for instance,
I agree that there should be some means of detecting |
I understand Blink has announced intent-to-implement for this I was asked to add an explicit description to this issue of why I feel this way. The primary reason is that I maintain the LABjs dynamic script loader. It has various feature tests (and, unfortunately, inferences) in it for exploiting different preloading techniques (of sorts) in browsers, both new and old. If However, there must be a feature detect for this. I will not be using any new features in LABjs that do not have direct and reliable feature tests (no inferences or UA parsing or any of that junk). Hopefully that underscores the necessity of a feature-test. :) |
@getify, the problem is that there is no guarantee that In other words, I don't see what you would gain from feature testing, rather than assuming with certainty that, being a "web standard", you are increasingly likely (over time/on new browsers) to get the desired effect when the browser's environmental conditions are right to perform the a preload. |
That doesn't match up with the semantics from my reading of the spec. If that's true, how is "preload" any different from "prefetch"? That is, how is there any less guarantee that
AFAICT, priority wouldn't affect the decision to use the feature or not. In fact, that might be a benefit to be able to opt into a system for script loading which is lower priority than
How?
I would absolutely expect that a browser should not make the feature-detect (whatever it is) return But the possibility of misbehavior on the part of browser vendors is in no way an argument against features designed and relied upon in good spirit. |
@marcoscaceres I am also confused about your "no guarantee" statement here. In terms of feature detection, one alternative could be exposing some |
@hexalys oh dear, maybe I'm getting my prefetch, preload, prerenders confused again :/ Ignore me. |
@marcoscaceres @hexalys @getify yep, Re, feature detect: the
If we had such an API, it wouldn't be too farfetched to also ask for "is preload supported?" |
It could be an API extended to check support for all/any Link Types or associated i.e. A more limited |
@hexalys For |
@igrigorik Understood. But this could be a |
@getify open to kicking off a discussion on whatwg on feature testing ? :) |
@getify perfect, thank you! Let's see what we can come up with there. |
I like where this is going. Semantically, feature detection and testing is 2 things:
These are requirement of detection/testing seems valuable for hints. |
@zcorpan's suggestion on the WHATWG thread makes a ton of sense for preload, and @zcorpan - that's not something that's currently part of the link element spec, right? |
No, it would be a spec change. |
For reference, from the whatwg thread:
^ sgtm. @zcorpan should we open a bug for this on whatwg? |
Thanks for this thread. I'm trying to research how this might come together in a real implementation. Mind having a look, @igrigorik ? (Moved to an issue over at loadCSS to keep this thread on topic.) |
@scottjehl commented on the issue; lgtm. In related news, opened a bug for the HTML spec: |
We don't have a label for "blocked on other spec", hence marking as 'help wanted'. |
Had @travisleithead from TAG review this and there was no pushback. Nicely done, @yoavweiss ! |
@yoavweiss kudos on getting https://code.google.com/p/chromium/issues/detail?id=553945 fixed. Do you have a 'best practice' code snippet I should put in the spec? |
I'll PR something in a bit |
What did we actually end up with for the feature detection? The snippet indicated here: https://www.smashingmagazine.com/2016/02/preload-what-is-it-good-for/#feature-detection ... that really sucks if that's the required pattern for feature detect, having to wrap in a slowish |
Maybe: function supportsToken(token) {
return function(relList){
if (relList.supports && token) {
return relList.supports(token);
}
return false;
}
};
var supportsPreload = supportsToken("preload");
var rl = document.createElement("link").relList;
console.log(supportsPreload(rl)); One could make a more generic variant of the above if necessary. Is that what you mean? |
No, I meant that in the linked-to code snippet, a |
Ah, I'm concerned about that too. I hope it can't throw. That would be sad. |
The If you're calling |
@yoavweiss are you suggesting that |
@getify yeah, assuming |
It always throws for e.g. |
on IE |
If |
@yoavweiss that's a feature inference, not a feature detection. maybe parsing semantics, but I think it's important to be clear on such things. |
While I agree that there could be a case of an implementation implementing preload without making sure its feature detection mechanism is shipped as well, I'm not aware of such a case. Therefore, I think the distinction makes very little difference in practice. |
In practice? Maybe not much. It makes a bigger semantic difference though. From a documentation and code-readability perspective. |
If |
created separate issue "spec shuold mention that relList must also be implemented if preload is implemented" #126 or feel free to close that and reopen this one. |
here is a simpler version of @yoavweiss's snippet (https://gist.github.com/yoavweiss/8490dabb3e0aa112fc74) tailored just for this specific use case
given that there is no need for try/catch for this case (as he confirmed). Worked, and did not throw in any browser I tested with (IE 10+, safari 10.1+, chrome 50+, firefox 50+, edge 15+) |
How do I feature test if a new browser supports
link rel=preload
?The text was updated successfully, but these errors were encountered: