-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
createGlobalStyle creates memory leak in Next.js #3022
Comments
in our next.js project, we can only use one createGlobalStyle, other global styles is replaced with next.js <style global jsx> |
Yes we are having the same issue in our Next app as well. Only difference is that we are using latest Next version. |
We confirmed the rollback resolved the issue. Our current style file is just a copy of normalize css and reset css with some font-face definitions. Something like export const AppGlobalStyle = createGlobalStyle` @font-face { Where reset and normalize are something like const normalize = |
One thing I noticed today. We have a block comment in our globalstyle. I wonder if this is possibly related to #3018 |
We are running StoryShots over our 2000+ stories and after upgrading to Styled Components v5 our tests are timing out. I've been able to bring down the test time drastically by stripping our global styles (with |
We just had to revert a PR that added our first call to Using The contents of the css in |
@JoshAmos Yes this is exactly what happened to us. |
@ghengeveld are you still seeing that with 5.0.1? 5.0.0 had a subtle bug in it relating to SSR that could cause some weird lag. We also have some unreleased changes in |
@probablyup, the issue is still there :( Packages versions: One of our global styles:
Memory usage with Memory usage without For sure we have some other small memory leaks, but Constructor: The object referred to the constructor ^^: As you can see the memory is being actively spammed on each page render by CSS string produced by
|
I am also able to verify this. I have been suffering from a memory leak in my Next.js app and after plenty of searching came across this issue which appears to be the cause of my problems. I am using Package versions: Downgrading You can see my I'd be happy to create a repo if the maintainers would find it useful in reproducing the issue? |
…vanilla CSS file due to flickering on page load in production and ongoing memory leak issues with Next.js styled-components/styled-components#3022 styled-components/styled-components#2134 vercel/next.js#7598
Has anyone been able to resolve this? I wonder if the issue is specifically with Next.js rather than styled-components. For example would any large string located in |
This is not related to Next.js. We're experiencing the same problems with a custom built React SSR implementation. Downgrading to version 4 solved the issue. Seeing the same picture in my debugger as #3022 (comment) |
We ran into a similar issue where stylis EDIT: Confirmed. EDIT 2: retracting confirmed, the memory is still rising on our server, but slightly slower. EDIT 3: A heap snapshot: EDIT 4: mistook the last one, it was still on SC 5.0 due to one of our component libraries. This is the memory snapshot from 4.4.0: Yep works fine when deployed: |
Same problem on SSR with Next.js and Styled-components v5.1.1. Could not downgrade, so workaround was just not to use |
Out of interest what was your workaround? An alternative approach to using |
Just a string in style tag, put in head:
Using Typescript, so had to cast an any type to use this component as before UPD: One can also use just a .css file (in case of Next, which supports them out of the box), but we have variables there, so was not an option in my case. |
Need a reproduction |
I think it's going to be quite hard to reproduce without access to one of our hosting environments. Can you check my heap dump photo above? That could elude to the issue. |
I have tried to create a bare bones (no frameworks) reproduction case, but I could not reproduce memory leak. Maybe I'm doing something wrong, so someone can take a look and update this repo: https://github.com/Oikio/styled-components-memory-leak-reproduction. On our production we have Nextjs and lots of things happening of course, but I could see allocation of strings from createGlobalStyle growing. If I will find time later, I will try to create reproduction with Nextjs. |
We ran into this issue on production. Like most others indicated, this was due to Versions: Will update when I have a reproduction available. |
@Oikio what does your global style look like, are there a lot of dynamic interpolations? |
|
@probablyup only 3 styles, they all look similar, not much happening there, I've even tried to remove all code from them, so that it would just a string passed to
|
Still not much progress in finding our why
It looks pretty straightforward and I wanted to blame Next.js first, until I saw that version 4 was fine and also there is a comment from peeter-tomberg above, having same issue with non Next.js project. BTW, @peeter-tomberg if you can share how styled-components are integrated in your app without Next, it would great for invsetigating the issue. |
Further update, id argument in I'm still struggling to understand the flow of the calls to make clear picture of why this is happening. |
Allocation always increments id here:
But previous id for
At least it looks like the issue. I'm not sure where to fix it. |
You can mount multiple instances of a global style in your application and we use ID to track the individual invocations so when they're unmounted they're removed in the proper order. |
Hm, I don't see removing happening here for previous ids on new calls, where can I look at it? |
Now I'm getting it a bit. Looks like this bug is a bit more than just styled-components. It's not reproducible with Next unless one adds apollo-client for graphql. Looks like in our app Reporters, do you use |
@Oikio Yes, but how is it related to the problem? The issue happens when we migrate from v4 to v5 using |
That's what I will try to figure out. At least hope so, I'm already running out of time for the ticket I'm on. |
@Oikio Yes, I am also using apollo-client and facing this issue. |
Looks like it's happening because when using
Instead |
Good question for the Apollo team I guess 🤷♂️ |
It could be something dumb like mismatched React versions in the tree leading to mismatched context symbols or something like that. Have you tried forcing a consistent version of react and react-dom via yarn resolutions? |
Version mismatch leads to lots of errors in the runtime, so it shouldn't be it, but I will check it out tomorrow and will take a closer look how context should be provided. |
So I'm giving up on properly solving this issue, I can't figure out how to pass context in my particular case with Nevertheless I have found a workaround to mitigate this issue:
Just wrapping it with |
@Oikio that's fine to do, it's essentially what |
By the way this issue should go away with Suspense coming to React SSR and Apollo, not that it's soon, but at least if my solution works for others with same problem - we should be fine. |
Closing, since I don't think there is a proper way to resolve this with the current implementation. |
Environment
Binaries:
npmPackages:
Problem
We are using styled-components and Next.js (7.2.1) and we have experienced memory leaks in our app for some time, so I started to investigate it, in the process I found other issues here about:
@import
insidecreateGlobalStyle
So I went through the whole app making sure to avoid these. I deployed a new version but the memory leak was still present. As seen on the graph below:
After inspecting the node server I found out it was
createGlobalStyle
in our_app.js
(Next.js file) that caused the memory to rise. In thecreateGlobalStyle
we usedfontFace
/normalize
from polished), and around 50 other lines of reset css (nothing special).Removing
createGlobalStyle
solved the memory leak (or at least reduced to a minimum).See the graph below:
Did we use
createGlobalStyle
wrong or do someone have some insight to this?The text was updated successfully, but these errors were encountered: