single-spa / import-map-overrides

A browser and NodeJS javascript library for overriding import maps
MIT License
335 stars 47 forks source link

Failed to execute `attachShadow` on `Element` in 3.0.0 #66

Closed nilzona closed 11 months ago

nilzona commented 2 years ago

In the new version 3 I'm getting the following console error

Uncaught DOMException: Failed to execute 'attachShadow' on 'Element': Shadow root cannot be created on a host which already hosts a shadow tree.
    at a.value (https://cdn.jsdelivr.net/npm/import-map-overrides@3.0.0/dist/import-map-overrides.js:2:45424)
    at a.value (https://cdn.jsdelivr.net/npm/import-map-overrides@3.0.0/dist/import-map-overrides.js:2:45188)

This shows up when I am using single-spa-react to render one react microfrontend on the page.

Is there a way to avoid that? @ralphsaunders maybe you have an idea? It's most likely related to https://github.com/joeldenning/import-map-overrides/pull/65

OriolInvernonLlaneza commented 2 years ago

We have the same problem on page load.

OriolInvernonLlaneza commented 2 years ago

It looks like the issue comes from the element already having a shadow root attached. It makes attachShadow throw InvalidStateError exception.

I made a PR that just checks if it is already created, it seems to work fine but maybe I didn't take something into account.

smilyanp commented 2 years ago

We have the same problem

moderndegree commented 2 years ago

I am seeing the same error. @joeldenning, is there any chance we can get someone to look at this?

lhtdesignde commented 2 years ago

Seeing the same thing. Uncaught DOMException: Operation is not supported. Happens at value:function(){this.shadow=this.attachShadow({mode:"open"})

Works fine in 2.4.2. Would be great to get this fixed seeing that it's around for some time. @joeldenning any way we could help here?

SergioTx commented 1 year ago

Same error here with version 3.0.0.

adripanico commented 1 year ago

Any update on this?

Achilles1515 commented 1 year ago

Workaround until fixed:

<!-- <import-map-overrides-full show-when-local-storage="overrides-ui"></import-map-overrides-full> -->
<script>
    const imo = document.createElement('import-map-overrides-full', { is: 'import-map-overrides-full' });
    imo.setAttribute('show-when-local-storage', 'overrides-ui');
    const attachShadowOriginal = imo.attachShadow;
    imo.attachShadow = function (options) {
        return this.shadowRoot ?? attachShadowOriginal.apply(this, [options]);
    };
    document.body.appendChild(imo);
</script>
piwysocki commented 1 year ago

@joeldenning , can you release v3.0.1 with a fix?

robmosca commented 11 months ago

This has been released. Closing it.