-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Dynamic global styles with SSR issue #2158
Comments
I am having similar problems while using gatsby + emotion. Is there a way to control de order that emotion style tags are injected into the HTML? |
If you would not use the
There is only |
At present, my solution is to exclude export const wrapRootElement = ({ element }) => (
<ThemeProvider theme={theme}>
<CacheProvider value={global}>
<Global styles={styles} />
</CacheProvider>
<CacheProvider value={cache}>{element}</CacheProvider>
</ThemeProvider>
)
|
I have found the same issue. Here is a minimal reproduction (without MUI): https://github.com/oliviertassinari/mui-repro-global-styles. The bug was surfaced in mui/material-ui#24440 (comment). |
@oliviertassinari I believe it would be best to create This would also require some changes in My schedule is quite busy lately so it might take me a while before I could get to this - if your team could help out with this, that would be quite great. |
The workaround proposed by @zce seems to do it. <React.Fragment>
<Global
styles={{
body: {
backgroundColor: darkmode ? "#000" : "#fff",
},
}}
/>
<CacheProvider value={cache}>
<Component {...pageProps} />
<button onClick={() => setDarkMode((prevState) => !prevState)}>
switch to {darkmode ? "light" : "dark"} mode!
</button>
</CacheProvider>
</React.Fragment> The only downside is one invalid HTML markup. It's OK-ish for a temporary solution. The global style can be rendered before any other content of the body. The warning only happens once, not flooding / burying the other important potential errors too much. However, in the long term, we need to fix it. @Andarist We will keep it in mind, thanks for taking the time to share pointers on how you envision the solution. Maybe we can come back to it once we release v5 stable. |
cc @mnajdova in case you want to work on #2158 (comment) |
Current behavior:
style tags generated by
<Global />
components appear in front of style tags generated by SSR:As a result, the dynamically calculated global styles cannot override the styles in the SSR:
To reproduce:
https://codesandbox.io/s/snowy-violet-tq704
Expected behavior:
The style generated by SSR should appear first:
Environment information:
react
version: 17@emotion/react
version: 11The text was updated successfully, but these errors were encountered: