Closed
Description
Hi,
Again, thank you for your work on Emotion, it's a fantastic library.
UPDATE: Scroll down to this message, it better describe the issue.
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
srmagura commentedon Apr 16, 2022
Emotion inserts styles differently in React 18 to be compatible with React's new SSR streaming capabilities.
Could you explain what the expected behavior vs. actual behavior is for Emotion in the context of React 18? Ideally you would also provide a minimal reproducible example.
BTW, I tried running your project on Windows but could not because of the Unix-specific scripts.
Make test app runnable on Windows #78 emotion-js/emotion#2725
Make test app runnable on Windows #78 emotion-js/emotion#2725
garronej commentedon Apr 18, 2022
Hi @srmagura thank you for your answer,
As I am sure you know, MUI is using emotion.
With MUI, 0-config SSR isn't supported, to enable SSR, MUI users are expected to explicitly provide an emotion cache and manually insert the
<style>
tags in the<head>
as described in the "advanced approach" section of the emotion documentation.You can find here a canonical setup for MUI + Next.js. This approach no longer works with react 18.
Reproduce
I created a minimal repo to exhibit what is no longer working as it used to.
Code: https://github.com/garronej/ssr-react-18-next
Fist disable JavaScript
You'll see:

Now, if you downgrade to
react-dom
17.0.2
you'll get:What's happening
You can see this
<style>
tag inserted at the end of the<head>
should take priority overcolor: read
generated bycss
here.The red string tag is not generated with
react-dom
17 and causes thegreen
style to be overwritten.My 2 sence
If you run the app with
yarn dev
the bug happens only the very first render but we can see that withreact-dom
18 unlike withreact-dom
17 the_app.tsx
is run twice and the first time it's beforegetInitialProps()
of our custom_document.tsx
has been called.Screen.Recording.2022-04-18.at.02.22.21.mov
In the hope that a solution can be found,
Best
srmagura commentedon Apr 18, 2022
@garronej Thank you so much, that really clears it up.
The "Advanced Approach" docs seem to be incomplete or incorrect for React 18 at the moment. Someone will need to do some testing with Next and MUI and either fix or add to that section in our docs.
Apart from the aforementioned documentation issue, I don't see anything Emotion is doing that is "wrong". My current feeling is that MUI is not compatible with the new streaming SSR, and that that is the underlying issue.
After reading the relevant code in Emotion (which is quite complex), I think Emotion will always insert
<style>
tags into the<body>
when using React 18, even if the Advanced Approach is used and SSR streaming is not used. (I could be reading the code wrong though.) In React 17 when using the Advanced Approach, Emotion does not directly insert<style>
tags during SSR, but our docs recommend you manually place them in the<head>
.Maybe we need to insert a toggle into Emotion to allow users to revert back to the React 17 behavior even when using React 18? I am thinking of this as a stopgap until MUI can fully support SSR streaming.
A lot of the discussion on mui/material-ui#24109 is likely relevant to this.
Andarist commentedon Apr 19, 2022
I've created a Next.js issue about this here.
As a workaround you set
clientSideEmotionCache.compat = true
here. However, I think that the setup used in this repro case is slightly weird - you probably shouldn't useCacheProvider
in yourMyApp
. When combined withDocument.getInitialProps
this will result in wrapping the whole thing twice with theCacheProvider
and you don't pass the "outer" cache to the innerCacheProvider
- I would suspect this leading to some unexpected results.Andarist commentedon Apr 22, 2022
The created issue was closed by this PR so I will assume that this should get fixed with the next Next release.
garronej commentedon May 3, 2022
Hi,
Next 12.1.6 just released.
I confirm that it solves the issue.
himadrinath commentedon May 12, 2022
looks like the problem arrived again in canary release.
can any one confirm that
garronej commentedon May 12, 2022
Hi @himadrinath,
I just tested with the current canary version,
This specific issues is fixed, there is no regression.
However, there are other issues, see the warning of tss-react: