kit: Hydration failing with DOMException: Failed to execute 'appendChild' on 'Node': This node type does not support this method.
Describe the bug
When using a sveltekit project with the new svelte 5 preview, I’m getting a hydration error when trying to pass child elements. This error occurs both when using <slot/> and {@render children()}.
This only seems to occur if a few conditions hold:
- The child elements contain one of
td,tr,th,tbody,tr,theadand you render the child elements in a#eachblock - You nest a svelte component within itself (e.g.
<Div><Div>hi</Div></Div>)
Reproduction
Here’s a stackblitz reproduction of the problem: https://stackblitz.com/edit/sveltejs-kit-template-default-xzcrsi?description=The default SvelteKit template, generated with create-svelte&file=package.json,src%2Froutes%2FTable.svelte,src%2Froutes%2F%2Bpage.svelte,src%2Fapp.d.ts&title=SvelteKit Default Template
After opening the stackblitz link, open devtools and note the error message.
Basic reproduction steps:
- Have a component that uses either
<slot/>or{@render children()} - Create an instance of that component
- For the children of that instance, use a
#eachblock, rendering any table element (td,tr, etc) - You should see the error in the console
I have another reproduction using the nested element approach here: https://stackblitz.com/edit/sveltejs-kit-template-default-8bn5tx?description=The default SvelteKit template, generated with create-svelte&file=package.json,src%2Froutes%2F%2Bpage.svelte,src%2Froutes%2FDiv.svelte&title=SvelteKit Default Template
Logs
chunk-E4357WXT.js?v=176774cb:3494 Hydration failed because the initial UI does not match what was rendered on the server. DOMException: Failed to execute 'appendChild' on 'Node': This node type does not support this method.
at Module.child (https://sveltejskittemplatedefaultxzcr-nwsl-ybr6fjpf--5173--420f2654.local-corp.webcontainer.io/node_modules/.vite/deps/chunk-E4357WXT.js?v=176774cb:162:12)
at https://sveltejskittemplatedefaultxzcr-nwsl-ybr6fjpf--5173--420f2654.local-corp.webcontainer.io/src/routes/+page.svelte:31:19
at https://sveltejskittemplatedefaultxzcr-nwsl-ybr6fjpf--5173--420f2654.local-corp.webcontainer.io/node_modules/.vite/deps/chunk-E4357WXT.js?v=176774cb:2856:7
at execute_signal_fn (https://sveltejskittemplatedefaultxzcr-nwsl-ybr6fjpf--5173--420f2654.local-corp.webcontainer.io/node_modules/.vite/deps/chunk-E4357WXT.js?v=176774cb:3794:7)
at execute_effect (https://sveltejskittemplatedefaultxzcr-nwsl-ybr6fjpf--5173--420f2654.local-corp.webcontainer.io/node_modules/.vite/deps/chunk-E4357WXT.js?v=176774cb:3898:31)
at schedule_effect (https://sveltejskittemplatedefaultxzcr-nwsl-ybr6fjpf--5173--420f2654.local-corp.webcontainer.io/node_modules/.vite/deps/chunk-E4357WXT.js?v=176774cb:3961:5)
at internal_create_effect (https://sveltejskittemplatedefaultxzcr-nwsl-ybr6fjpf--5173--420f2654.local-corp.webcontainer.io/node_modules/.vite/deps/chunk-E4357WXT.js?v=176774cb:4372:5)
at render_effect (https://sveltejskittemplatedefaultxzcr-nwsl-ybr6fjpf--5173--420f2654.local-corp.webcontainer.io/node_modules/.vite/deps/chunk-E4357WXT.js?v=176774cb:4440:10)
at each_item_block (https://sveltejskittemplatedefaultxzcr-nwsl-ybr6fjpf--5173--420f2654.local-corp.webcontainer.io/node_modules/.vite/deps/chunk-E4357WXT.js?v=176774cb:2853:19)
at reconcile_indexed_array (https://sveltejskittemplatedefaultxzcr-nwsl-ybr6fjpf--5173--420f2654.local-corp.webcontainer.io/node_modules/.vite/deps/chunk-E4357WXT.js?v=176774cb:585:17)
mount @ chunk-E4357WXT.js?v=176774cb:3494
createRoot @ chunk-E4357WXT.js?v=176774cb:3422
Svelte4Component @ chunk-E4357WXT.js?v=176774cb:5079
(anonymous) @ chunk-E4357WXT.js?v=176774cb:5058
initialize @ client.js?v=176774cb:288
_hydrate @ client.js?v=176774cb:1836
await in _hydrate (async)
start @ start.js:22
(anonymous) @ (index):27
Promise.then (async)
(anonymous) @ (index):26
System Info
System:
OS: Linux 5.0 undefined
CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Memory: 0 Bytes / 0 Bytes
Shell: 1.0 - /bin/jsh
Binaries:
Node: 18.18.0 - /usr/local/bin/node
Yarn: 1.22.19 - /usr/local/bin/yarn
npm: 9.4.2 - /usr/local/bin/npm
pnpm: 8.9.2 - /usr/local/bin/pnpm
npmPackages:
@sveltejs/adapter-auto: =2.1.1 => 2.1.1
@sveltejs/kit: =1.27.4 => 1.27.4
svelte: =5.0.0-next.4 => 5.0.0-next.4
vite: =4.4.0 => 4.4.0
Severity
blocking an upgrade
Additional Information
No response
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 15
- Comments: 27 (7 by maintainers)
Commits related to this issue
- feat(TweetMediaVideo): wrap video in window check The video component in TweetMediaVideo.svelte is now wrapped in a check for 'window' to avoid server-side rendering issues. This is a workaround for ... — committed to ryoppippi/sveltweet by ryoppippi 2 years ago
- feat: Add esm-env for browser detection This commit introduces the 'esm-env' package to the project. It is used in 'TweetMediaVideo.svelte' for browser detection, replacing the previous method of che... — committed to ryoppippi/sveltweet by ryoppippi 2 years ago
I just encountered the issue while toying around with slot snippets in a table. The error message should probably contain a bit more detail as to what caused it.
I just tested the original reproduction with the newest versions and get the following errors:
Firefox:
Uncaught (in promise) DOMException: Node.appendChild: Cannot add children to a TextChrome:Uncaught (in promise) DOMException: Failed to execute 'appendChild' on 'Node': This node type does not support this method.which could be possibly related to: https://github.com/sveltejs/svelte/issues/12422
Reproduction: https://stackblitz.com/edit/sveltejs-kit-template-default-6zahg7?file=package.json
Ran into this issue, and it was simply that I was using the HTML
<body>tag in my file. Replaced it with a div and it all worked.@Coronon your issue is related to the HTML being invalid, in the sense that the browser needs to repair the DOM, which means the DOM structure is different to what Svelte expects while hydrating, resulting in that error. If you change your
Table.sveltefile like this:it will work.
@ryoppippi please provide a reproduction (or point to an existing one which you refer to)
This happens to me when trying to render a component inside an Each block
In this case, I’m trying to render around 3k icons (svg) components. But I did a test with only 2 and the same thing happens. Those 2 components were a footer and a header.
Other example to “match” the Svelte Docs example 🤣:
I also tried wrapping them in a getter and it gives the same result.
UPDATE:
<tag attr={var}></tag>when i leave the tag without dinamic attrs then it doesn’t fail. It works the same when the attribute is hardcoded.i got this issue when using sveltekit2 and svelte5 for video tag The weird thing is that this issue happens even tho I don’t use snippets
Similar behavior with
mode-watcher. It injects some Script into the head, which is likely the reason.An issue exists, which has been closed “as there is supposed to be backward compatibility”.
I have a similar problem. I have an SEO component that writes something to svelte:head, and in the same component, I am loading three other components that do the same, so all four are writing something with svelte:head. error: RR_SVELTE_HYDRATION_MISMATCH: Hydration failed because the initial UI does not match what was rendered on the server. Error: this={…} of svelte:component should specify a Svelte component.
@dummdidumm I’ve managed to get the same error, this time using only
<div>elements. The error seems to be due to nesting the elements.Here’s the recreation: https://stackblitz.com/edit/sveltejs-kit-template-default-8bn5tx?description=The default SvelteKit template, generated with create-svelte&file=package.json,src%2Froutes%2F%2Bpage.svelte,src%2Froutes%2FDiv.svelte&title=SvelteKit Default Template