Extend feature detection with isIntersecting #211
Comments
|
It's possible to polyfill the isIntersecting property. The polyfill should use the native implementation where available, and polyfill in the missing property. |
|
|
|
I think it would something like:
|
|
The relevant edge bug is filed here. Any progress on this? |
|
There are some reports that, on Chrome anyway, there are cases where So I was wrong when I said |
|
That polyfill apparently doesn't work either: tootsuite/mastodon#3502 (comment). Any ideas? |
Yes, this can happen when the target is on the boundary of root but doesn't overlap at all. This is the spec'ed behavior. As for polyfilling the I'm also not so keen on changing the logic to determine whether the polyfill should apply because that would mean all Edge users would get the polyfilled version instead of the native version, even in cases where the application code doesn't depend on the For the moment, I think a fair compromise would be for developers who depend on the <script>
if ('IntersectionObserver' in window &&
!('isIntersecting' in window.IntersectionObserverEntry.prototype)) {
delete window.IntersectionObserver
}
</script>
<script src="/path/to/intersection-observer-polyfill.js"></script> |
|
@philipwalton This approach would also work for me.
The only problem I see with this approach ( E.g. the browser support info of the polyfill and caniuse claim full support in edge, which means that devs most likely won't be aware of it and only discover it by accident. I could propose a PR to at least share this information (including your |
Yes, we'd definitely want to update the README with this at minimum. You can PR or I can do that after we decide which action to take.
It occurred to me this morning that we could go about this the opposite way and default to spec-compliant behavior while still allowing site authors to opt-in to the native implementation. In other words, the polyfill would be used for any browser not implementing the <script>
if ('IntersectionObserverEntry' in window &&
!('isIntersecting' in IntersectionObserverEntry.prototype)) {
IntersectionObserverEntry.prototype.isIntersecting = () => {
throw new Error('isIntersecting is not implemented and shouldn not be called');
};
}
</script>
<script src="/path/to/intersection-observer-polyfill.js"></script> |
|
I'm also good with the opposite way. The thing I'm not really into is that devs would have to deal with this on their side because I wouldn't expect that when I'm dropping in a polyfill. Couldn't we move an additional stubbing and notification logic into the polyfill itself? Having it inside the polyfill would mean that people that just dropped in the polyfill would then be notified and lead back to the given docs sections (or even this issue) // Exits early if all IntersectionObserver and IntersectionObserverEntry
// features are natively supported.
if ('IntersectionObserver' in window &&
'IntersectionObserverEntry' in window &&
'intersectionRatio' in window.IntersectionObserverEntry.prototype) {
!('isIntersecting' in IntersectionObserverEntry.prototype)) {
Object.defineProperty(window.IntersectionObserverEntry.prototype, 'isIntersecting', {get: () => { throw new Error('isIntersecting is unfortunately not supported. Go here for details.'); }})
}
return;
}Edit: Hmm... but we might not throw here, because that might be backwards incompatible for people using the polyfill already. (maybe just a log message) |
|
I just spoke with the dev who implemented IntersectionObserver in Edge, and he said:
I haven't thoroughly tested yet, but apparently we have confirmation that Edge's implementation is different from Chrome's in that it won't report |
|
Also FWIW we have been using |
Cool, if it matches the native Edge implementation then I see no reason not to. The only other case I can think of is when an element starts out on the boundary of the root element (as opposed to "transitioning from a non-intersecting state"). According to the spec such elements should be |
|
I just wrote a small repro to test Edge's behavior vs Chrome, as well as Firefox with IntersectionObserver enabled in TL;DR: In the case of Edge, yes the polyfill of using if ('IntersectionObserver' in window &&
'IntersectionObserverEntry' in window &&
'intersectionRatio' in window.IntersectionObserverEntry.prototype &&
!('isIntersecting' in IntersectionObserverEntry.prototype)) {
Object.defineProperty(window.IntersectionObserverEntry.prototype, 'isIntersecting', {
get: function () {
return this.intersectionRatio > 0
}
})
}Unfortunately, though, there appear to be a number of inconsistencies between Chrome, Edge, and Firefox's implementations, as you can see in this screenshot: Non-matching behavior:
I'm not sure if this is something that should be hammered out at the spec level or not. There also appear to be no web-platform-tests for IntersectionObserver, so perhaps this would be a good opportunity to write some. I'm gonna go talk to our dev team about what to do about this; may also be good to alert Firefox folks to potential compat issues. |
|
OK I stand corrected on web-platform-tests; Gecko and Chromium have open PRs for it. (web-platform-tests/wpt#4384 web-platform-tests/wpt#6216) |
|
Making sure notifying in the same order as observers in Firefox has been worked on in https://bugzilla.mozilla.org/show_bug.cgi?id=1359311. We had some stability issues with related code in the past, thats why we decided to do some extra rounds of testing before landing that patch, which might lead to some delays here. But I expect it to land soonish. |
|
Good to know. FWIW we discovered another potential interop issue around firing events when |
|
@nolanlawson with these new interop issues, has your thinking changed on the polyfill updates for |
|
@philipwalton I'm in the process of filing a lot of bugs and working out what direction we want to go in. Expect my team to open up some spec issues. As for |
|
I forked the compat discussion into #222. Let's keep this thread about the polyfill for |



I discovered that according to caniuse Edge 15 supports the Intersection Observer API. While playing around with it I discovered though, that Edge does not implement the
isIntersectingproperty.The Firefox implementation (behind a flag currently) is also missing this property on
IntersectionObserverEntry.Would it make sense to extend the feature detection which is currently:
with a check for this property? I think it's really handy and this property missing could lead to "unexpected behavior" on developer's side.
Thanks in advance. :)
The text was updated successfully, but these errors were encountered: