-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
[fix] Svelte for Web Components DOMException: Failed to execute 'define' on 'CustomElementRegistry': this name has already been used with this registry #7353
Conversation
When using svelte to create webcomponents components, I found a problem. For example, I created an Icon component first, and then used it for both the Dialog component and the Button component. At this time, an error will be reported saying that the Icon component has been repeatedly defined. To find the cause of the error in the source code of svelte, just modify this code to: ""if (component.tag ! = null) { body.push(b !@_customElements.get("${component.tag}") && @_customElements.define("${component.tag}", ${name}););
fixed customElements.define error
I've been pushing the use of Svelte for future javascript components, mainly web components. However, this issue is preventing that from occurring. Why can this simple change not make it into the build? |
Thank you for the PR! |
@baseballyama This still does not answer my concern. If I am going to start using Svelte in a large project that is loading web components dynamically, then I need to know a way to fix this issue. It appears fix needs to be in svelte, since the only hook to state a component is a WC is via The rest is complied into Javascript. If this proposed PR is not the correct way, then how else to solve? |
the docs said...
https://svelte.dev/docs#run-time-custom-element-api Could you please tell us why the above way is not your solution? |
@baseballyama That is all and good but I am loading the pre compiled web component’s javascript file, so what would I use as the function (2nd parameter) to thre customElements.define. I will say that no other frameworks such as angular or react requires the consumer to control registering. |
As I said before, using Svelte as my go to javascript ‘library’ is useless without this! |
We can implement a register function like this.
I'm not sure about this question. import MyElement from './MyElement.svelte';
import SomeElement from "./SomeElement.svelte";
const elements = { MyElement, 'my-special': SomeElement };
const toKebabCase = (tagName: string) => {
return tagName
.split(/(?=[A-Z])/)
.join("-")
.toLowerCase();
};
export const defineCustomElements = (customElementNames?: Record<keyof typeof elements, string>) => {
Object.keys(elements).forEach((element) => {
// I don't recommend to use try/catch here, but you can ignore the error without this PR.
try {
customElements.define(
customElementNames?.[element] || toKebabCase(element),
elements[element]
);
} catch (e) {
console.error(e);
}
});
}; |
Anyway, I think this PR is not right fix, so I close this now. |
Before submitting the PR, please make sure you do the following
[feat]
,[fix]
,[chore]
, or[docs]
.Tests
npm test
and lint the project withnpm run lint