Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

customElement register error #4470

Closed
6 tasks done
agileago opened this issue Aug 2, 2021 · 12 comments · Fixed by #4475
Closed
6 tasks done

customElement register error #4470

agileago opened this issue Aug 2, 2021 · 12 comments · Fixed by #4475

Comments

@agileago
Copy link

agileago commented Aug 2, 2021

Describe the bug

when i mixin multiple vite project in one vite project(micro frontend). customElements overlay register will reregister and error

image

Reproduction

https://github.com/agileago/vite-mfe

export const overlayId = 'vite-error-overlay'
customElements.define(overlayId, ErrorOverlay)

System Info

chrome

Used Package Manager

yarn

Logs

No response

Validations

@agileago
Copy link
Author

agileago commented Aug 2, 2021

maybe we could add conditional when register

if (!customElements.get(overlayId)) {
  customElements.define(overlayId, ErrorOverlay);
}

@higuaifan
Copy link

I create a discussions a few days ago: #3911...
but nobody answered...

wish this issue can change something...

@patak-dev
Copy link
Member

maybe we could add conditional when register

if (!customElements.get(overlayId)) {
  customElements.define(overlayId, ErrorOverlay);
}

This sounds good. Would you like to create a PR @agileago? We should check if this is enough, and all the micro frontends are able to use the first registered one, but it seems that it will be fine to me

@sionzee
Copy link

sionzee commented Aug 2, 2021

I think you are fixing wrong issue here. You are loading @vite/client twice, rather than adding a condition we should prevent loading it twice.
#4023

@agileago
Copy link
Author

agileago commented Aug 3, 2021

@patak-js someone has done. please check it. thanks @webfansplz

@patak-dev
Copy link
Member

We should first resolve the comment from @sionzee to justify the need for this check

@sionzee
Copy link

sionzee commented Aug 4, 2021

@agileago I have got an email with your removed message. It is a good practice to strikethrough the text rather than removing it completely.

Anyway, we are on the same side. We too uses two vite applications but the vite client is being injected by the code during import. Unfortunately nothing which could be somehow prevented without touching vite source code.

Using the workaround mentioned in the example above could run into next issues for e.g.: triggering hot updates twice, and soo on. That's because all vite handling will be there twice and more times if using more applications.

@sionzee
Copy link

sionzee commented Aug 4, 2021

@patak-js in my opinion Vite shouldn't inject the client whatever is happening. Developer is responsible for adding the client import to the page. I believe by removing the auto injection it will resolve this and other similar issues. The docs also doesn't mention the auto injection so it behaves as a bug. If it must stay there due compatibility then adding an option to turn it off will be really welcomed.

@patak-dev
Copy link
Member

Vite shouldn't be auto injecting in this case, but maybe there is an issue with the setup of the OP

@github-actions
Copy link

github-actions bot commented Aug 4, 2021

Hello @agileago. Please provide a minimal reproduction using a GitHub repository or codesandbox. Issues marked with need reproduction will be closed if they have no activity within 3 days.

@sionzee
Copy link

sionzee commented Aug 4, 2021

@patak-js Sounds like tree-shaking is not working here:
https://github.com/vitejs/vite/blob/main/packages/vite/src/node/plugins/css.ts#L286

This line is served to browser and it tries to import client.mjs

where the:

const overlayId = 'vite-error-overlay';
customElements.define(overlayId, ErrorOverlay);

is being called.

In my scenario the client.mjs is included by .vue file (will update soon with more details)

image
Here you can see the client is called from EditorView.vue, and first line in EditorView.vue is: (added by vite)

import { createHotContext as __vite__createHotContext } from "/editor/esm/@vite/client";import.meta.hot = __vite__createHotContext("/src/vue/EditorView.vue")

That is another link to @vite/client.


I have tried to remove both references for the @vite/client and the error is not happening anymore.

@agileago
Copy link
Author

agileago commented Aug 5, 2021

@patak-js https://github.com/agileago/vite-mfe

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants