Skip to content

Custom Element rendered markup is removed during hydration #9772

@dritter

Description

@dritter

Describe the bug

At work we use a couple of webcomponents (custom elements) from different teams. We chose webcomponents to be framework agnostic so they work everywhere. If these webcomponents render markup that markup gets removed on the client side when svelte takes over control. This does even happen, if I mark this component as custom element in vite (= has no effect, thus not included in reproducer):

diff --git a/vite.config.js b/vite.config.js
index bbf8c7d..0a479e1 100644
--- a/vite.config.js
+++ b/vite.config.js
@@ -2,5 +2,11 @@ import { sveltekit } from '@sveltejs/kit/vite';
 import { defineConfig } from 'vite';
 
 export default defineConfig({
-   plugins: [sveltekit()]
+   plugins: [sveltekit({
+       template: {
+           compilerOptions: {
+               isCustomElement: (tag) => tag.startsWith('my-component')
+           }
+       }
+   })]
 });

Pointers in reproducer

The custom element gets defined in app.html and is used in +page.svelte.

Workaround

It works, if I wrap the component in @html (I guess that disables the hydration):

{@html '<my-component></my-component>'}

Maybe that could be done automatically if svelte detects a custom element?

Other tries

I tested this even with sveltejs/svelte#8457 and it still happens. It would be nice, if this case could be considered in this PR.

Reproduction

  1. git clone https://github.com/dritter/sveltekit-custom-element-reproducer
  2. cd sveltekit-custom-element-reproducer
  3. npm i
  4. npm run dev -- --open
  5. Observe that the red element gets removed as soon as the client side takes over
Bildschirmaufzeichnung.vom.2023-04-19.09-54-12.webm

Logs

No response

System Info

System:
    OS: Linux 6.2 Arch Linux
    CPU: (8) x64 Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
    Memory: 2.87 GB / 15.39 GB
    Container: Yes
    Shell: 5.9 - /usr/bin/zsh
  Binaries:
    Node: 19.9.0 - /usr/bin/node
    npm: 8.19.2 - /usr/bin/npm
  Browsers:
    Chromium: 112.0.5615.165
    Firefox: 112.0.2
  npmPackages:
    @sveltejs/adapter-auto: ^2.0.0 => 2.0.0 
    @sveltejs/kit: ^1.5.0 => 1.15.5 
    svelte: ^3.54.0 => 3.58.0 
    vite: ^4.2.0 => 4.2.1

Severity

annoyance

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions