fix: remove memory leak from retaining old DOM elements #11197
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This was an interesting one to fix, happy to take feedback on tweaking the implementation.
Today, we have a common memory leak retaining DOM elements with fragment templates. The block effect has
dom
property with is just the[...fragment.childNodes]
and that might seem innocent, but it's actually quite the opposite. That's because the template fragment we clone is populated as render effects and other logic blocks consequently add content to the fragment as we go (each blocks, if blocks etc). If that content gets cleared, it's never removed from thedom
array and is stored forever.We don't need to do this though. Instead we can capture the effect
dom
at the start of the template and only push into template nodes which we no belong only to that given effect. That means we stop pushing in DOM elements from other blocks – with the exception of HTML and dynamic elements – which don't get their own template.I also encountered a bug in the existing system with dynamic HTML blocks in each blocks. This PR also fixes that too!