-
Notifications
You must be signed in to change notification settings - Fork 238
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
Emotion example is poorly performant, re-rendering over 50 times a second #1
Comments
Hey @3x071c, can you share here how did you solve this issue 🤔 ?. Thanks! |
@PJColombo If you pass an empty dependency array to |
I see. Thank you for the explanation @3x071c. All these performance problems keep users from using the template and makes the styled-component app example a better CSS-in-js remix alternative in my opinion. Let's keep the issue open until it get's solved or improved. |
Why is all the boilerplate in the emotion example even necessary? It's just working out of the box for me. |
Yes thank you I've read the docs... Is that suggesting that the point of this whole boilerplate is to allow nth child and similar selectors? |
@tavoyne Using emotion in the "out-of-the-box" SSR mode doesn't require an example, while the more advanced approach does. If one prefers the secondary approach for whatever reason (more control, better support etc.), I think they should get a proper example for that, just like Next.js (et al) do in their examples. |
Thanks for clarifying that. Well, the only thing we have for now is a broken, contextless example. It could be wise to rename it That kind of context is crucial for adoption, IMO. Most people using emotion don't have a clue about how it works in the background. |
@tavoyne We'd be more than glad to review a PR with the changes you've mentioned! |
@machour That's what I was planning to do. I just hoped that we could discuss things beforehand. |
@tavoyne I don't think an |
I'm not sure to agree. The point of examples is to help newcomers, not to confuse them. You mentioned Next.js, note that it does have a basic example (which indeed is quite trivial) but has no advanced one. About the fact that it's off topic, I apologise. I thought this was about making the emotion example better. |
@tavoyne The basic example you linked actually does extract and inline critical CSS using "advanced" SSR techniques (definitely more simple than what the emotion docs suggest though). This is the true basic example, which as you can see is so stupidly simple (just install the dependencies and use the library) that there's no reason for one (but if you feel like it, you can of course submit a PR). This "basic" approach comes with many downsides that are not mentioned in the emotion docs and are the reason why so many users and maintainers reach for the "advanced" approach instead (see these notes by the official MUI maintainer, the chakra-ui docs (a UI framework which uses emotion) etc.). I opened this in the hope of preventing more people from worsening their own app when getting chakra-ui/mui/emotion up and running with Remix, and I think providing a full example that promotes spec compliance and other problems from arising should be the default/go-to approach in the Remix examples. |
I've been playing with the example for a brief time and saw this error too. First, Emotion on SSR (as styled-components) injects all the styles after reading the requested components so the server understand what to render and finish the job. This means that the styles that we create it's being send to the server and then to the browser. By knowing that, every time that we assign The only instance that this is happening, it's on the client // Only executed on client
useEffect(() => {
// avoiding unwanted rendering
if (document.head == emotionCache.sheet.container) return
// ... At start it will take the first requested styles and, by testing this I haven't had problems with assigning new styles, removing styles/components removing nodes from function calls and going back/forward on the browser and styles are re-rendering whenever is necessary and performance issues were gone. I hope to not to miss something? |
I agree with @tavoyne. I just wanted to see if remix works with emotion and the first thing I did was to look for this example which I consider is the ideomatic way of integrating Emotion with Remix. It has confused me for sure. |
If someone would like to update the emotion example to mirror this chakra example that would be awesome: #35 Emotion supports SSR with streaming and we should be using their native helpers instead of trying to hack our own solutions: https://emotion.sh/docs/ssr#renderstylestonodestream |
I plan on knocking this out today 🙂 |
I ran into this same issue, everything seems to work fine without out this problematic code (including nth child selectors): Lines 35 to 53 in cc3e057
The emotion SSR docs to show anything similar to this that currently, maybe they used to? |
@chaance Hi, did you manage to figure this out? I was just stumbling over this issue trying to use MUI with Remix and there are so many similar but not quite the same examples out there how to use Emotion with Remix, it's quite confusing 😕 |
@ePirat @jacob-ebey @chaance Just tell Remix to not take over the entire |
@3x071c hey, where does React warns about hydrating the full document? Couldn't find that reference. |
Lines 52 to 53 in c8833ea
The present example is in fact, not correct. It causes a re-render loop by every time updating the context state in @3x071c I came here looking for whether Emotion works with Remix, and have found the answer to be no. The documentation is extremely negative about css-in-js, and the example is broken. MUI, for example, is pretty popular; it is short-sighted to tell its users to go away. Personally, I am scooting back to next.js where I came from. @machour Not sure about official docs to the effect, but using |
I've managed to make it work somehow by adapting the instructions from Chakra UI. |
We're using MUI w/Emotion in Remix following this example. It works with SSR (initial response is styled without/before JS) and after the page is hydrated At one point we needed the That Chakra UI example looks nearly identical and is easy to follow so I'd give that a shot |
Would be lovely if someone submitted a PR to the example 🙏🏼 |
I've been meaning to do this but it fell to the bottom of my to-do list. I'll try to get back on it next week if no one else beats me to it 😅 |
@MinervaBot This isn't a Next.js vs Remix thing, though it is a defaults/approach issue. It's not a Remix-specific issue, rather telling React to hydrate the entire document in general is a bad practice. Once you change the DOM root container setting up emotion (and anything based on it) is as simple as following the official emotion SSR docs. The project I linked above implements this successfully. |
@machour It used to be in the docs, can't find it anymore. But the warning is still thrown when you let React take over |
I just opened #78 to fix this issue. |
The way the |
This issue has been automatically closed because we haven't received a response from the original author 🙈. This automation helps keep the issue tracker clean from issues that are unactionable. Please reach out if you have more information for us! 🙂 |
Just wanted to call out that this is still broken, and #78 does resolve the issue. |
@RobinClowers Emotion assumes the head element is not controlled by React, but in Remix the entire document is controlled. This is a fundamental problem which does not really have a great solution. We evaluated Remix and decided "no thanks" for this reason. |
What version of Remix are you using?
1.2.3
Steps to Reproduce
$ svn export https://github.com/remix-run/remix/tree/main/examples/emotion
$ vim app/entry.client.tsx # Add `console.log("🚨 RERENDER 🚨")` in `reset()` function body
$ npm run dev
This is not only re-applying the style tags, but also re-rendering the entire app tree over 50 times a second, which isn't good at all. See my comment below for a suggestion on how to combat this problem.
Expected Behavior
A fast app
Actual Behavior
An app that is flooded with console warnings and re-renders
The text was updated successfully, but these errors were encountered: