material-ui: Next.js + Material UI v5 styled-component example is still showing className mismatch error
- [ x] The issue is present in the latest release.
- [ x] I have searched the issues of this repository and believe that this is not a duplicate.
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.
Prop
classNamedid not match. Server: āsc-dlnjwi fBCjsW MuiBox-rootā Client: āsc-gtsrHT kMdZqK MuiBox-rootā
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.
- Fork https://github.com/mui-org/material-ui/tree/75f27074b89381d25a1fab85fc2098af58e89cb0/examples/nextjs-with-styled-components-typescript
- Create a simple styled component
- Restart server (if you are using codesandbox).
- See console for error.
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`
System:
OS: macOS 11.4
Binaries:
Node: 16.5.0 - ~/.nvm/versions/node/v16.5.0/bin/node
Yarn: Not Found
npm: 7.19.1 - ~/.nvm/versions/node/v16.5.0/bin/npm
Browsers:
Chrome: 92.0.4515.107
Edge: Not Found
Firefox: Not Found
Safari: 14.1.1
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 30
- Comments: 48 (16 by maintainers)
Commits related to this issue
- adde Footer and fixed mui/material-ui/issues/27512 — committed to PomoFriends/pomofriends-app by vadimegorov13 2 years ago
Still having this issue even with the latest examples. Any updates here?
Hi guys, anyone figured out a fix?
https://github.com/mui-org/material-ui/tree/master/examples/nextjs-with-styled-components-typescript has some recents commit, still seeing the errors when developing
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.
~https://github.com/mui-org/material-ui/pull/27088#issuecomment-885278495 is a working solution.~
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.
The problem with this approach is that the SSR part ends up being turned off @Michal-gasiorowski, Iām having the same problem using next + material-5 + makeStyles + npm, I saw several issues reporting this type of issue being caused by various reasons, this ended up leaving me confused about the real status of this issue?
_document.tsx
`import { ServerStyleSheets } from ā@mui/stylesā
MyDocument.getInitialProps = async (ctx) => { const sheets = new ServerStyleSheets() ctx.renderPage = () => originalRenderPage({ // eslint-disable-next-line react/display-name enhanceApp: (App: any) => (props) => sheets.collect(<App emotionCache={cache} {ā¦props} />), }) ⦠return { ā¦initialProps, // Styles fragment is rendered after the app and page rendering finish. styles: [ā¦React.Children.toArray(initialProps.styles), sheets.getStyleElement()], } }`
Guys, I have edit sandbox example, because it doesnāt reproduce bug. To reproduce bug, styled component must use props
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 If u dont use props, it is ok, but once u have used props based styles, bug happens
Our temporary solution until we will migrate from makeStyles
Hi again! I was able to get it working with yarn v2.
Followed the implementation in https://github.com/mui-org/material-ui/pull/27583 until I got the problem above, but I solved that problem by upgrading
babel-plugin-styled-componentstov1.13.3, and it seems to solve the problem overall. Cannot see any warnings any more!After struggling for a day with a combination of issues (Next.js 12 + MUI v5 + TypeScript + makeStyles), I guess there is a permanent solution (at least for those using emotion as engine and are wiling to use
styledapi). I think it also works with styled components but canāt be sure since I ran into another error (same mentioned by @mnajdova here);So⦠to the possible solution:
While trying to fix the media query thing, I stumbled upon this CodeSandbox, that uses the
styledAPI instead ofmakeStyles. I also noticed that it uses a<StyledEngineProvider>component withinjectFirstproperty, but after testing it, the use ofstyledAPI was enough to prevent the issue.I will send a PR, adding a styled component to the the emotion example in the next minutes (my first PR here, still unsure of how to proceed, but Iāll give it a try), so that other people can also avoid wasting time on the
makeStylespath.@mnajdova, Iām still unsure of how to tackle the issue but it is worth mentioning that the issue is the same, following both,
styled-componentsandemotionexamples.Detailed steps to reproduce:
(Terminal) Clone any of the available examples of nextjs + mui + typescript, (emotion or styled-components)
(Terminal) Install dependencies:
(Terminal) Add
@mui/stylesdependency to usemakeStyles:(/pages/index) Import makeStyles and define
classes:http://localhost:3000;responsiveand drag to force the media query responsive font size changes;My environment:
node: v16.13.0 yarn: v1.22.10
But it is happening with emotion.
just use emotion, thereās no issue with emotion.
See https://github.com/mui-org/material-ui/issues/29742#issuecomment-982597676 I am starting to think that we should maybe rather recommend emotion for SSR projects. There are just so many issues using
styled-componentswith external library.Credits to @Janpot for the investigation around this.
@mnajdova Thanks for the answer, I tried to use Yarn but still got the same error after one refresh
Warning: Did not expect server HTML to contain a <style> in <div>.In every page refresh