next.js: styled-components with Babel plugin results in className mismatch on Next.js 5
- I have searched the issues of this repository and believe that this is not a duplicate.
Expected Behavior
styled-components should not result in the className mismatch warning from React.
Current Behavior
Warning: Prop `className` did not match. Server: "Header-johx6c-0 bHIeEO" Client: "Header___default-johx6c-0 gGUBlx"
I’m exporting the header component like so:
export default styled(Header)`
text-align: center;
`;
Notice that styled-components uses the name Header on the server and Header__default on the client. This doesn’t happen with Next.js 4, so I assume it has something to do with the way modules are built with the new Universal Webpack setup.
Steps to Reproduce (for bugs)
- Use
styled-componentswithnextv5. - Use
babel-plugin-styled-componentswith the defaultdisplayNamesetting. - Render a component exported like:
export default styled(Component)
(It doesn’t matter what the Babel plugin’s ssr setting is, nor if the server stylesheet is set up in _document.js. Same thing in every combination.)
Your Environment
| Tech | Version |
|---|---|
| next | v5.0.0 |
| node | v8.9.4 |
| OS | macOS |
| browser | Chrome |
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 16
- Comments: 19 (10 by maintainers)
I suppose this is a side effect of using ES module exports for the client and CJS for the server, correct?
I thought we used to have a fix for this in place but apparently we don’t. I’m not sure how safe it’d be to filter out component names that are exposed as “default” but that’d be a solution.
Please note that I would not recommend the “export default styled.div” pattern. I’d guess this is what causes this.
As the result you’d get when applying the above to arrow functions, similarly for our Babel plugin this would mean that your StyledComponent technically doesn’t have a name. So simply writing “const X = …; export default X” might help here if I’m not mistaken. Even if for “Header” you’d name that variable “Header” or “Container”, for debugging in general this would be more explicit.
Hmmm, I don’t use different envs in my .baberc (actually it looks like the one Tim referenced) and I don’t export the styled component. I just use it in the same file:
Strangely enough it happens only on my index page and not on every page load…
@kitten makes sense. The example is actually fine since it doesn’t use
"env"maybe this should be documented in the example readme though 🤔https://github.com/zeit/next.js/blob/canary/examples/with-styled-components/
https://github.com/zeit/next.js/blob/canary/examples/with-styled-components/.babelrc
@timneutkens Actually it still happens. Is it just me or do you guys also still have this warning popping up?
I’m using
next@7.02andstyled-components@4.1.1.I haven’t said anything like this at any point 🤔
Maybe @kitten knows what’s going on.
For me everything is fine in production but I get a warning for className mismatch in development. I quickly solved the issue by disabling stylesheets server-side rendering in development.
Here is my _document
Then you just to have to edit your babel config to disable the
ssroption in development (note that if you’re using a.babelrcfile to manage babel’s config you’ll have to convert it to a.babelrc.jsfile)Tim knows this and said its on their list of things todo very soon!