Polyfill-service: Weird feature request: force polyfill in certain browsers

Created on 14 Mar 2018  Â·  13Comments  Â·  Source: Financial-Times/polyfill-service

My case: I'm using IntersectionObserver, and it's great in Firefox, and the polyfill works great in all the other browsers.

Chrome's IntersectionObserver implementation is really bad, though. Wildly inaccurate, not very usable for my purposes.

So: I want to be able to use polyfill.io in such a way that the polyfill will be loaded normally in all browsers, but always loaded in Chrome.

I have no idea how common this use case is. I couldn't find an existing issue, so I'm making this one. I understand that it's probably a stretch to expect it to be implemented.

All 13 comments

As a workaround, you can check if your code is running in Chrome and then delete IntersectionObserver so the polyfill kicks in:

delete window.IntersectionObserver;
delete window.IntersectionObserverEntry;

That wouldn't work with the polyfill service because it would use user-agent detection to determine that the polyfill didn't need to be shipped to the browser.

You can ask the polyfill service to always send the polyfill, independent of whether the browser already supports the feature, by appending the flag |always to the feature. If you also append the flag |gated, the polyfill will not overwrite existing browser implementations.

The docs about the |always flag:

Polyfill should be included regardless of whether it is required by the user-agent making the request.

Open the polyfill in Chrome (no flags; you will see no polyfill):
https://polyfill.io/v2/polyfill.min.js?features=IntersectionObserver

Open this polyfill again in Chrome (with flags, you will see the polyfill):
https://polyfill.io/v2/polyfill.min.js?features=IntersectionObserver|always|gated

Right, but I don't want to download the polyfill in browsers like Firefox where the native version works just fine.

Check the issue title - this is a very specific feature request to force a polyfill to be included in a specific browser where it normally wouldn't be included.

It won't overwrite the built in implementation unless you delete it.

@rlightner I believe that is only true when using the gated flag. https://polyfill.io/v2/docs/api

https://polyfill.io/v2/polyfill.js?features=IntersectionObserver&flags=always,gated includes:

// Exits early if all IntersectionObserver and IntersectionObserverEntry
    // features are natively supported.
    if ('IntersectionObserver' in window &&
        'IntersectionObserverEntry' in window &&
        'intersectionRatio' in window.IntersectionObserverEntry.prototype) {
      return;
    }

Yes - but only because you included the gated flag in the parameters.

(For what it's worth, this has nothing to do with my original request)

without gated, it produces the same thing:

// Exits early if all IntersectionObserver and IntersectionObserverEntry
// features are natively supported.
if ('IntersectionObserver' in window &&
    'IntersectionObserverEntry' in window &&
    'intersectionRatio' in window.IntersectionObserverEntry.prototype) {
  return;
}

Your original question was about forcing it on chrome. Which as @ChristianSiegert pointed out would work if you deleted it in chrome before the polyfill loaded. It's not the best implementation though.

Let's hope the 'powers that be' see a feature request out of this thread.

Huh, I wonder if that's a bug, or if the documentation is wrong.

Sure, I would have no problem manually deleting the original implementation in Chrome.

All that this feature request is requesting is the ability to say "always ship the polyfill in Chrome, but default behavior for the other browsers." Nothing about gating behavior.

you could user agent sniff before loading the script, and use |always
without |gated on that version of Chrome.

and use the non-always URL for everyone else.

On 30 May 2018 at 20:55:01, Josh Duff ([email protected]) wrote:

Huh, I wonder if that's a bug, or if the documentation is wrong.

Sure, I would have no problem manually deleting the original
implementation in Chrome.

All that this feature request is requesting is the ability to say "always
ship the polyfill in Chrome, but default behavior for the other browsers."
Nothing about gating behavior.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/Financial-Times/polyfill-service/issues/1682#issuecomment-393296430,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAK4WvUfiCQ4hez_onNMpY0P-prcJoHNks5t3vkVgaJpZM4Sqz3e
.

I don't think we would be interested in adding a feature to enable users to configure a polyfill to always load in specific browsers.

If a browser has a buggy implementation of a feature, we should update our polyfill configuration to send a polyfill to that browser.

If you could open an issue on https://github.com/Financial-Times/polyfill-library with the bugs you have found in IntersectionObserver for Chrome, I will happily write tests for those and have the polyfill get served to those specific versions of Chrome :-)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

michapietsch picture michapietsch  Â·  6Comments

kentcdodds picture kentcdodds  Â·  5Comments

SebastianS90 picture SebastianS90  Â·  8Comments

JakeChampion picture JakeChampion  Â·  4Comments

LeoIannacone picture LeoIannacone  Â·  7Comments