Skip to content
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

[fix] Removal of empty stylesheets created from transitions #7260

Closed
wants to merge 6 commits into from
Closed

[fix] Removal of empty stylesheets created from transitions #7260

wants to merge 6 commits into from

Conversation

MathiasWP
Copy link
Contributor

@MathiasWP MathiasWP commented Feb 13, 2022

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with [feat], [fix], [chore], or [docs].
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with npm test and lint the project with npm run lint

Closes

What has been changed?

The style_manager "cleaned up" the stylesheets it made by only clearing their rules. After clearing them, it removed them from its internal map by running managed_styles.clear(). This cleanup was not optimal - it left the style elements empty in the dom.

The proposed change is to remove style-elements created by style_manager, instead of making them empty and letting them stay in the DOM without any references. I also refactored some of the methods from the runtime/internal/dom.ts file that were only used by the style_manager or in the dom.ts file itself.

If you want to reverse-engineer this bug yourself, then start with the append_empty_stylesheet method in the dom.ts file. As long as append_empty_stylesheet is used, there will be bugs related to empty stylesheets. It does the following:

  1. Creates an empty style-element
  2. Appends it to the dom
  3. Returns only the sheet of the new style-element, leaving the style-tag totally unreferenced by any code

@MathiasWP MathiasWP changed the title Updated style_manager cleanup to remove created stylesheets instead o… [fix] Updated style_manager cleanup to remove created stylesheets instead o… Feb 13, 2022
@MathiasWP MathiasWP changed the title [fix] Updated style_manager cleanup to remove created stylesheets instead o… [fix] Updated style_manager cleanup to remove created stylesheets instead of leaving them in the DOM Feb 13, 2022
@MathiasWP MathiasWP changed the title [fix] Updated style_manager cleanup to remove created stylesheets instead of leaving them in the DOM [fix] Updated style_manager cleanup to remove created stylesheets instead of only making them empty Feb 13, 2022
@MathiasWP
Copy link
Contributor Author

I saw that the actions failed during npm install, and tbh I am not sure what triggers the error. It seemed like a declaration mismatch, so i removed the return-type i set on the function. Seems like a silly fix if it works. Is it because of a difference between TS versions?

@MathiasWP
Copy link
Contributor Author

Any updates on this PR? 👍

@MathiasWP MathiasWP changed the title [fix] Updated style_manager cleanup to remove created stylesheets instead of only making them empty [fix] Correct cleanup for empty stylesheets that were appended to head after transitions Feb 22, 2022
@MathiasWP MathiasWP changed the title [fix] Correct cleanup for empty stylesheets that were appended to head after transitions [fix] Correct cleanup for empty stylesheets appended to head from transitions Feb 26, 2022
@AshfordN
Copy link

Given that this PR is attempting to patch a memory leak in a common feature (transitions), I'm hoping it would be addressed soon.

@MathiasWP MathiasWP changed the title [fix] Correct cleanup for empty stylesheets appended to head from transitions [fix] Removal of empty stylesheets created from transitions Apr 8, 2022
@MathiasWP
Copy link
Contributor Author

MathiasWP commented Apr 12, 2022

@Conduitry it would be much appreciated getting some thoughts about this PR from some of the core Svelte-maintainers. It's my first contribution, so I am not sure how to follow this up 🤔

@johndunderhill
Copy link

It's important to get this fixed. We shouldn't be polluting the DOM like this. This is blocking our use of Svelte transitions. We will not use Svelte transitions until this is fixed.

@TrungRueta
Copy link

Hello, i think this PR is ok, can we merge it for next version release? for now leak style tag hurt system a lot

MathiasWP and others added 2 commits May 8, 2022 15:40
Co-authored-by: Yuichiro Yamashita <xydybaseball@gmail.com>
Co-authored-by: Yuichiro Yamashita <xydybaseball@gmail.com>
@MathiasWP
Copy link
Contributor Author

Thank you for the great help, @baseballyama, I appreciate it! Committed your suggestions 👍

@lukas-nyberg
Copy link

Any updates on this? Just ran into it :)

Copy link
Member

@baseballyama baseballyama left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added one comment , but this is basically LGTM.
Thank you!

Copy link
Member

@baseballyama baseballyama left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM💪

@MathiasWP
Copy link
Contributor Author

@baseballyama I've noticed something that we need to address before merging: I cannot get the test to fail anymore. Is there anything wrong with how i used the raf.tick methods? One of your commits was changing it, but i didn't think that would affect the end result.

@baseballyama
Copy link
Member

I cannot get the test to fail anymore. Is there anything wrong with how i used the raf.tick methods? One of your commits was changing it, but i didn't think that would affect the end result.

Could you please tell me more details?
Now I just quickly run the test for both master and your branch.
And master is failed and your branch is succeeded.

After that I updated tick like below and test was failed on your branch.

raf.tick(0);
raf.tick(0);

@MathiasWP
Copy link
Contributor Author

I cannot get the test to fail anymore. Is there anything wrong with how i used the raf.tick methods? One of your commits was changing it, but i didn't think that would affect the end result.

Could you please tell me more details? Now I just quickly run the test for both master and your branch. And master is failed and your branch is succeeded.

After that I updated tick like below and test was failed on your branch.

raf.tick(0);
raf.tick(0);

To be honest I'm not sure why it doesn't fail. If it seems to work elsewhere then I guess it's okay. I just wanted to be sure that nothing was wrong.

@baseballyama
Copy link
Member

@MathiasWP

Maybe you rebased master branch, that's why this PR is closed automatically.
I open the PR now.

@tanhauhau
Copy link
Member

tanhauhau commented Jul 5, 2022

@MathiasWP is this branch still around? somehow couldn't clone this to test it out: https://github.com/MathiasWP/svelte/tree/style_manager-stylesheet-cleanup

@MathiasWP
Copy link
Contributor Author

@MathiasWP is this branch still around? somehow couldn't clone this to test it out: https://github.com/MathiasWP/svelte/tree/style_manager-stylesheet-cleanup

Oh, i don't think so actually! Sorry! I deleted this fork and created a new one - totally forgot that this PR was up. What can i do to solve this best for you? @tanhauhau

@tanhauhau
Copy link
Member

probably that's why the PR was closed previously.

What can i do to solve this best for you?

Maybe can see if u could recreate the branch from this commit 662faa3 ?

@MathiasWP
Copy link
Contributor Author

probably that's why the PR was closed previously.

What can i do to solve this best for you?

Maybe can see if u could recreate the branch from this commit 662faa3 ?

I'll do this sometime this week! Any release deadline i should know about?

let i = stylesheet.cssRules.length;
while (i--) stylesheet.deleteRule(i);
const { style_element } = info;
detach(style_element);
info.rules = {};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can also be removed

@MathiasWP
Copy link
Contributor Author

@tanhauhau i recreated the fix on my fork.

I also removed some more code, e.g. the line i commented above. But it does not seem like the change is added in this PR. But you can fork my repo and check out the source code 👍

@tanhauhau tanhauhau closed this Jul 6, 2022
@tanhauhau tanhauhau reopened this Jul 6, 2022
@tanhauhau
Copy link
Member

sorry im just trying to see if close and reopen fixed that 🙈

@tanhauhau tanhauhau closed this Jul 6, 2022
@tanhauhau
Copy link
Member

recreated the pull request over at #7662

@MathiasWP
Copy link
Contributor Author

recreated the pull request over at #7662

I'm sorry for this inconvenience! It was silly of me to remove the fork, i totally forgot that it would affect this PR...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants