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
Uncaught (in promise) TypeError: Cannot read property 'removeChild' of null #6037
Comments
My issue is not reproducible with svelte 3.17.3 |
So the latest svelte version is 3.34.0, is it fixed since 3.17.3? Or its a regression bug on 3.34.0? |
I have this behavior with Unfortunately, I can't share my repo, and I don't find the root cause to isolate the issue and create a repl. |
Sapper-based project, this in a .svelte file crashes the app and requires a reload on navigating away from the relevant Sapper route:
The element whose parentNode no longer exists is an IFRAME and I assume it has been added to the DIV to help measure width and/or detect resizes. It seems that the logic cleaning up I attempted a demo here but I don't know how to make it un-mount the component like it does during navigation - or perhaps there's some other trigger I have not discovered. I hope this helps anyway. |
On my side, I'm not on sapper, just Svelte. I don't even know where to look at to help >< |
Do you know what element it crashes when it tries to remove? In devtools, you can set a conditional breakpoint on the line that throws and make the condition !element.parentNode - then it will stop just before throwing. |
Thx a lot @hallvors , I never use this debugging way! It's in a component where I have:
=> no parentNode. I updated this component like this:
And now, I don't have the error anymore. I tried to reproduce here https://svelte.dev/repl/747b8a3626624afab7f1640635190591?version=3.34.0 But I don't manage to have my not working behavior. Not sure what to do now. Should we close the issue? Or? |
I think the Svelte developers likely want to fix something here, so let's leave that decision to them :) |
If it can help, you should take into account the parent creating this component in order to reproduce this issue. IIRC there are a lot of "anchoring" cases when your component has only a condition on the top level, and the anchor is provided by the parent. |
let me check if understand your situation correctly, <script lang="ts">
....
</script>
{#if showEdit}
...
{/if} and it occurs when you toggling on/off of the Maybe can explain further on how this component is being used by the parent
|
@tanhauhau it's already a component. Let me try to explain with a drawing showing the different steps: On my project, and in browser debug mode (thx @hallvors) I have the steps 3.1 and 4.1 added So I'm closing the first Dialog before the second one. Notes:
In the end, I think that it's me and my own logic and not really an issue for Svelte. |
#2086 (comment) We do similar things like you: Open closing dialogs and overlays and changing big parts of the DOM. We have a pretty interactive page (mini games) and we are using a lot of different components which are dynamically shown and hidden all the time. |
Just in case it's useful to anyone else, here's what specifically caused this issue for me and how I solved it: My code:
Basically, I have a sortable table where I wanted to place an icon next to the header of the column being sorted. I'm using Iconify icons and inserting them using |
Hello! We're seeing a lot of issues with users using Google Translate. Some cases we've solved by simply adding "notranslate" classes to affected components, but we'd really like to avoid spraying that all over our app. If you want to see it in action, you can reproduce one case like this:
|
Hello there ! |
Thanks for this workaround. Instead of configuring rollup to change output bundle, I use https://www.npmjs.com/package/patch-package - this works both for local development and for production bundle |
Related to @apop880 comment (#6037 (comment)), I encountered this issue recently when using fontawesome icons that I did not have wrapped in tags - once I wrapped them in tags, went away. Was not an issue previously but I am unsure how many versions ago that was. |
In my case, this error appears if I am sending HTML to a component slot. So Also the HTML in the elements tab looks wrong to me: |
I am encountering the same issue. I am working with |
I have a repro of this issue involving Twitter embeds that are rendered within a modal where embed code is rendered using https://github.com/pushred/svelte-issue-6037-repro/blob/main/src/routes/index.svelte (freshly initialized SvelteKit project, only changes are in the above file) I suspect this is due to the Twitter's My repro also has an option that demonstrates the workaround of wrapping |
This seems to be a regression bug.
I'm using maplibre-gl and trying to make markers utilize slots for their HTMLElement property, but when cleaning up the marker it removes the element before Svelte does and Svelte gets confused. Best solution would be just to return the original fix.
|
From version 3.38.1 onwards, a regression was introduced on Svelte, more specifically on commit 6d16e92 (which was previously fixed on issue #2086), due to this, svelte version will be locked on the latest working version (3.38.0) until it's fixed More details: sveltejs/svelte#6037 (comment) Credits to @byt3rr for the in-depth information on this issue
https://svelte.dev/repl/0d826c0d67c54d8693aeaa1b77ea09ef?version=3.44.2 Very simple REPL to reproduce this error. Just click the buttons in order Not sure how the suggested/previous solution should've caused side effects though Here is a test for svelte that would fail due to this => https://github.com/TorstenDittmann/svelte/tree/fix-destroy-missing-fragment/test/runtime/samples/destroy-missing-fragment |
I encountered this bug in a similar context — trying to toggle/destroy a custom Marker component on a mapbox map, built with beyonk/svelte-mapbox library. Likewise, the fix that @byt3rr specified from @ciri worked for me. Change line
It appears the line number to make this change (currently
And replace that function with:
|
Thank you @sbutler-gh , this just saved me big time. |
In some scenarios a null error can occur (see below) - think it may be this issue and resolution: sveltejs/svelte#6037 (comment) index.mjs:199 Uncaught (in promise) TypeError: Cannot read properties of null (reading 'removeChild') at v (index.mjs:199:21) at Object.d (ChannelElement.svelte:47:58) at Object.d (ChannelElement.svelte:46:9) at index.mjs:812:27 at n (index.mjs:18:12) at Array.forEach (<anonymous>) at i (index.mjs:24:9) at nt (index.mjs:793:9) at Object.p (ChannelElement.svelte:40:47) at X (index.mjs:764:36)
This problem is still happening witn the latest svelte. I'm not sure if there was a regression here: "svelte": "^3.46.6",
|
Please don't make "the bug is still happening" comments. We already know that because the issue is still open. |
Since this is a long running issue, with multiple issues opened for 3 years, I thought that maybe I did something wrong in my Svelte code. And I don't really want to patch the Svelte code. I tried to add multiple span or div to wrap various items like icons, nothing works. And I finally identified the root cause was a DOM.remove() I'm using to manage a list and front-run the Svelte mechanism to dismiss an element. Because on a table it is easier to control like that the contents and controlling out the animation. It was like that :
And the fix is :
To me, this issue is not an issue in Svelte, but an issue on how developers are using it. How does it happen ? When you have a DOM which is just inside an Svelte block, like {#if ..} or {#each ...}, and a JS script is removing this DOM. The removal is done by JavaScript outside of the knowledge of Svelte. At the end, when a page change (e.g. SPA routing), when Svelte kills all the DOM of the page, there's no element remaining in that Svelte block, and the crash occurs. It can be for example :
How to fix it? Basically, make sure there isn't any script removing a component or DOM directly inside a Svelte block. In a way, the core issue is that Svelte is unsynced with the DOM, and is not aware of a DOM removal, than when it closes it, the element was already removed and doesn't exist anymore. So wrap it as a child, or refrain to use any DOM removal from JS. My PoV is that this is not really an issue in Svelte. Still, adding Also, this can make it clear as a warning in the documentation. I haven't seek about this in the doc so far. |
Thank you @antonio-fr for the guidance. You put me on the right track after many hours of aimless debugging. If it helps anyone else, my situation was a top-level
Wrapping the entire block in a div resolved it for me: |
In my case, I also needed to the wrap the component inside the conditional block in an extra Before {#if isTextVisible}
<Text>Hello World</Text>
{/if} after <div>
{#if isTextVisible}
<div>
<Text>Hello World</Text>
</div>
{/if}
</div> |
Thanks @lukePeavey - this fixed it for me. This certainly seems like a bug in svelte in my case. |
I hit this bug again yesterday, when trying to add Google Maps JS API to a Svelte-based site. Even though I'm already following this issue and tried several workarounds it kept crashing and it was not at all obvious why. I was following some online guides adding the Google maps JS api with an actual (Sorry this comment is basically just a +1 and a rant, but it was rather frustrating. I think Svelte is an amazing concept with lots of potential..) |
Thanks, this was very helpful. Unfortunately, the problem is sometimes (as is in my case), other libraries that interact with Svelte will interact with the DOM so there really should be some standardized handling of this. Either way, the point you make is great (it should be an important note in the docs). |
Just chiming in to say that in our case we are experiencing this bug in relation to users using browser translators in places there we pass data into a svelte component without wrapping them in any html tags. for example the following code <Tooltip>
hello there
</Tooltip> creates no errors, except when the user translates the page using the chrome translate feature, then the tooltip throws However simply wrapping the what is passed into the slot with a html element <Tooltip>
<span>general Kenobi</span>
</Tooltip> Will solve the issue, so that even users using translate gets no error. My guess would be that because the browser translation replaces the content, if there is no html element to replace within, svelte looses the reference to the element. And when it needs to remove it it's unable to. Whether this is in actuality a Svelte issue or a browser translation issue I don't know - it is however extremely difficult to safeguard a larger app against passing data into slots without wrapping them in html elements - And in our case the entire app crashes so a critical issue. @Conduitry is your hesitence against #6910 related to performance? Could it be solved with a build check instead to make sure you are not passing anything into a slot without wrapping it in a html tag? |
The web is an open development platform - users may run extensions that do any sort of crazy things inside the DOM, they may even run bookmarklets or change the DOM directly in devtools. Svelte-generated code simply has to be prepared for the DOM changing underneath it, and IMHO it is definitely a bug in Svelte to crash because of a no-longer-valid reference to the DOM. |
@jycouet @sudomaxime @torgebauer @orgertot @shedali @clineamb @kilianso
if you could kindly post the following about your issues, so that we could better look into it:
yarn why svelte
ornpm list svelte
)The text was updated successfully, but these errors were encountered: