Skip to content

Custom elements are not "cleaned up" when the consuming application removes an element. #8191

Closed
@Pixelatedza

Description

@Pixelatedza

Describe the bug

Hey guys, it appears that custom-elements don't get cleaned up fully when a parent application removes them from the DOM. The issue croped up because of a crash in a child component, specifically a modal that is animated in.

At first I thought it was just the subscriptions to the stores that weren't being removed appropriately. However, in a more complex example, I have nested components with conditional rendering based on the store value, and this logic still gets executed. At least, I assume so, since the components crashing would not be rendered if said logic wasn't applied.

  • Site
    • custom-element
      • nested child with logic (subscribes to store)
        • conditionally rendered child (crashes because of missing stylesheet reference).

I assume this is because the components or their subscriptions were not fully cleaned up when the parent removed them from the DOM.

In my project I can work-around the error, for now, by removing the animation. Changing
<div class={`modal ${size}`} transition:fly={{ y: 1000, duration: 500 }}>
to
<div class={`modal ${size}`}>

The error: Uncaught (in promise) TypeError: Cannot read properties of null (reading 'insertRule'). I'm guessing this is because the stylesheet has been removed/cleaned up by either svelte or the browser, but the component still attempts to do it's thing (animate in), because the store value has changed.

Attached a video showing what I'm seeing for what it's worth. I'm running the minaml repo I've linked below.

svelte-bug.mov

Reproduction

Here is a minimal reproduction. You can run svelte as usual, yarn dev. It's in the /vite-project directory. Then open the index.html in the root of the repo in a browser.

If you toggle the content on and click on "change store value", the value of the store will be printed as many times as the content was shown.

https://github.com/Pixelatedza/min-svelte-wc-bug-demo

Logs

No response

System Info

System:
  OS: macOS 11.6.7
  CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
  Memory: 915.37 MB / 16.00 GB
  Shell: 5.8 - /bin/zsh
Binaries:
  Node: 16.15.1 - ~/.nvm/versions/node/v16.15.1/bin/node
  Yarn: 1.22.19 - ~/.nvm/versions/node/v16.15.1/bin/yarn
  npm: 8.11.0 - ~/.nvm/versions/node/v16.15.1/bin/npm
Browsers:
  Chrome: 109.0.5414.87
  Firefox: 108.0.2
  Safari: 15.6
npmPackages:
  svelte: ^3.54.0 => 3.55.1

Severity

blocking all usage of svelte

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions