WICG / inert

Polyfill for the inert attribute and property.
Other
924 stars 81 forks source link

How can I check if the polyfill is already loaded? #159

Closed fwebdev closed 3 years ago

fwebdev commented 4 years ago

I'm in an environment where parts of all Pages already have loaded the Polyfill. I don't want to load the Polyfill again on those pages by my Component.

timbomckay commented 3 years ago

We have a similar scenario where inert is getting pulled in multiple times and sets the mutation observers each time. I played around with this locally to figure out if there's a way to only set the InertManager when it hasn't been set yet and found it behaves properly if I move the instantiation into the prototype condition.

Currently

/** @type {!InertManager} */
  const inertManager = new InertManager(document);

  if (!Element.prototype.hasOwnProperty('inert')) {
    Object.defineProperty(Element.prototype, 'inert', {

Recommendation

if (!Element.prototype.hasOwnProperty('inert')) {
    /** @type {!InertManager} */
    const inertManager = new InertManager(document);

    Object.defineProperty(Element.prototype, 'inert', {

Not sure if this is the proper solution, but it's hurt us a couple of times and think it shouldn't create the mutation observers if they're already being observed.

robdodson commented 3 years ago

This sounds similar to #163

@timbomckay would you be interested in sending in a PR for that fix?

timbomckay commented 3 years ago

Ah yes. Meant to do that a while back but never got around to it. We implemented this in our repo and haven't had any issues since. I'll try to get a PR up today or tomorrow.