-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
Next.js + Material UI v5 styled-component example is still showing className mismatch error #27512
Comments
Agree, I don't think that we can expect developers to use the current version of the example. Personally, I wouldn't as it not really working.
As to why the current solution doesn't work? I don't know. It would be great to have a deep dive. So far, it seems that styled-components was not designed for SSR. |
Thanks @oliviertassinari I've tried your linked solution. I've removed the webpack config for alias, and updated package.json with It seems to have removed the className mismatch error. However, for your information. I cannot remove the Since MUI v5, I've been experiencing the following error if I don't use the next-transpile-modules. It says export is undefined. I haven't tested enough to know whether I'm the only one needing the transpile, or whether it's really Mui related.
|
@daveteu Do you have a reproduction? |
I also faced this issue recently with Next Js version 11.1.2 for some components. But I was able to overcome the issue using the dynamic imports used by Next Js with ssr:false Material UI version
Styled Component version
npx @material-ui/envinfo
|
As i mentioned in closed issue above, in a builded app these errors does not exists, but i faced some components rendering issues, like Tabs. In development environmnent the application crashes in every refresh, throwing this warning, the unique workarround that i found is using next/dynamic in every MUI component with SSR set to false, but, with this we loss the server side goods.
|
For me on
(and also having usual I'm styling with |
In every page refresh |
Putting "alias" in the package.lock surely wouldn't cause that. |
Is already in package.json, there's also in next.config.js the configs from examples.
Already deleted .next and node_modules, cleaned up the browsers data and didn't solve this issue. |
Guys, I have edit sandbox example, because it doesn't reproduce bug. Here is a link to see the bug https://codesandbox.io/s/charming-rain-oswex?file=/pages/index.js the same thing to withStyle and makeStyle |
This comment was marked as off-topic.
This comment was marked as off-topic.
Hi guys, anyone figured out a fix? https://github.com/mui-org/material-ui/tree/master/examples/nextjs-with-styled-components-typescript |
@alexhhn see #27512 (comment) If you are using |
dunno if this is some poor hack or what, but i found solution that works for me: _app.tsx:
|
its not recommend if you use because the docs said : https://mui.com/styles/basics/#main-content recommented using import * as React from "react";
import Typography from "@mui/material/Typography";
import { css } from "@emotion/react";
import { useTheme } from "@mui/material/styles";
export default function MiniDrawer() {
const theme = useTheme();
const classes = React.useMemo(
() => ({
titleAppbar: css`
color: ${theme.palette.customColor.blue};
`,
}),
[]
);
return (
<Typography css={classes.titleAppbar} variant="h6" noWrap component="div" >
Mini variant drawer
</Typography>
)
} |
Thats why i posted another solution, few posts below, that uses that approach and omit it completely when the bundle is built (using envs), as the issue seems not to appear on production build. Im using this approach since then without issues, and i can still use SSR on production. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
I had two deployments of the same app. When NODE_ENV was set to production this error occurred, whereas on my localhost and in a dev environment it didn't happen. Debugging this I found that in my production environment I was getting the dev version of react, because I wasn't threading NODE_ENV through my deployment steps correctly. In my case I am using import maps in the client and for the server-side rendering, I think that having NODE_ENV set to 'production' in my Node.js server, but rendering React SSR from the development import of React may have been causing this. But can't be sure. |
Our temporary solution until we will migrate from makeStyles import * as React from 'react';
import Document, { Html, Head, Main, NextScript } from 'next/document';
import createEmotionServer from '@emotion/server/create-instance';
import { lightTheme } from '../src/theme';
import createEmotionCache from '../src/createEmotionCache';
import { ServerStyleSheets } from '@mui/styles';
export default class MyDocument extends Document {
render() {
return (
<Html lang="iw" dir="rtl">
<Head>
{/* PWA primary color */}
<meta name="theme-color" content={lightTheme.palette.primary.main} />
<link rel="shortcut icon" href="/static/favicon.ico" />
{/* Inject MUI styles first to match with the prepend: true configuration. */}
{(this.props as any).emotionStyleTags}
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}
MyDocument.getInitialProps = async (ctx) => {
const originalRenderPage = ctx.renderPage;
const cache = createEmotionCache();
const { extractCriticalToChunks } = createEmotionServer(cache);
const sheets = new ServerStyleSheets();
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App: any) =>
function EnhanceApp(props) {
return sheets.collect(<App emotionCache={cache} {...props} />);
},
});
const initialProps = await Document.getInitialProps(ctx);
const emotionStyles = extractCriticalToChunks(initialProps.html);
const emotionStyleTags = emotionStyles.styles.map((style) => (
<style
data-emotion={`${style.key} ${style.ids.join(' ')}`}
key={style.key}
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{ __html: style.css }}
/>
));
return {
...initialProps,
styles: [
...React.Children.toArray(initialProps.styles),
sheets.getStyleElement(),
],
emotionStyleTags,
};
}; |
It is fun cause we are not using |
Still having this issue even with the latest examples. Any updates here? |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as resolved.
This comment was marked as resolved.
Does anyone have some updates on this issue? |
Not solved yet? |
Deleting |
This happens to me in development ( |
Works for me too |
Current Behavior 😯
I've followed the example for nextjs styled-components.
The example works out of the box.
However, once you create a styled-component, the className did not match from server side rendering error will occur.
https://github.com/mui-org/material-ui/tree/75f27074b89381d25a1fab85fc2098af58e89cb0/examples/nextjs-with-styled-components-typescript
Expected Behavior 🤔
The example should work like it should.
Steps to Reproduce 🕹
I've forked the example ( from above ) with code sandbox.
What I did was to create a simple styled component on index.js (I'm not proficient with ts) and the error will show.
I've done the above in a sandbox - https://codesandbox.io/s/jolly-yonath-kjxj2?file=/pages/index.js
Context 🔦
I'm using NextJS but I'm not sure how to modify your example to work with Material-ui v5.
I think this is the toughest part using NextJS + Material-UI and a solution is necessary.
Your Environment 🌎
`Material-UI`
"@material-ui/core": "^5.0.0-beta.2", "@material-ui/icons": "^5.0.0-beta.1", "@material-ui/lab": "^5.0.0-alpha.41", "@material-ui/styled-engine-sc": "^5.0.0-beta.1", "@material-ui/styles": "^5.0.0-beta.2",`NEXT.JS`
"next-transpile-modules": "^8.0.0", "next": "^11.0.1",`npx @material-ui/envinfo`
The text was updated successfully, but these errors were encountered: