-
-
Notifications
You must be signed in to change notification settings - Fork 141
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
Wrong styles applied during SSR hydration #351
Comments
You probably should use Should be confirmed by some of the maintainers. |
@javiertury This is the first time I've seen issues that speak very closely to the super annoying issues we are also having. Env: We too are noticing that our projects (@ourcompany/projectx) that consume our ui lib (@ourcompany/react-ui-components) are experiencing this same issue with the wrong styles being applied. The way we importWe're not using typescript, and never have imported this way in either our repos. Version of
|
@iDVB if the symptoms are similar to mine then it's probably because this babel plugin is not transforming your source files.
Even if you are importing the package correclty, there can be other reasons why your source files are not being transformed. From the description of your issue, I suspect that the problem lies in your babel configuration with respect to packages Please check that both babel and your bundler are configured to transform your https://babeljs.io/docs/en/options#exclude |
@javiertury Thanks! Ya I bet it's something like that. But can you clarify one thing that has been nagging us. As styled-component ui lib authors, are we supposed to apply the Currently, when publishing the ui lib we ARE using that plugin, however when we are bundling the consuming project (gatsby) we are ONLY targeting things NOT in node_modules. So this lib would not be processed this second time. Also, keep in mind everything works fine AS-IS so long as we keep to |
Beyond the scope of my setup, I can only provide ideas for configuration changes but not much more. Even though in many cases it's recommended to do transpilation at the library level, I have a gut feeling this plugin is different because there are reports of bugs when duplicated packages for styled-components or the babel plugin are used. I would say try to apply this babel plugin only on your main project(gatsby) and take the plugin out of your libraries (but just this plugin, you can keep the rest of your babel config). Try also to bundle those libraries as ES6 modules (that's the preferred format for rollup and webpack 5). Finally, take a look at other issues that may be related with other aspects of your setup, for instance this bug with duplicated imports because of symlinked node_modules. |
@javiertury makes sense. But the larger question I'm thinking.... is why did this work fine before with previous versions? |
@javiertury I'm a little confused about the conclusion of this issue. Are you reporting a bug or just confusion? Did @mnajdova's comment help? |
Now just confusion. Although at first I didn't know about Also I would like to draw attention to how painful it was to find out that re-exporting the package was breaking SSR. Perhaps the debugging experience could be improved somehow or the documentation for the plugin could include a reference to
I replicated the original problem in my project, used @mnajdova |
I had the same issue when using nx dev with NextJS and what worked for me was to
|
Styled components is applying wrong styles during SSR hydration. The problem is that styles in production builds are swapped across components. For instance, the class
sc-16rshtk
is applied to a ToolbarContainer but contains the styles of a FooterLink. I'm using gatsby with gatsby-theme-material-ui and gatsby-plugin-styled-components.This didn't happen for client side rendering, only for SSR. Since it only affected components styled with styled-components, I tried to collect more information by assigning a displayName to each component, but it did nothing, classnames remained the same.
Then I found #261 and saw this comment. It says that styled-components should not be re-exported to make displayName work.
After applying that change, I made a new production build and then discovered that SSR hydration was now working correctly. I suspect that babel-plugin-styled-components does not work if the package is re-exported.
This issue was very hard to debug.
If you were re-exporting the module to add theme type definitions, please use module augmentation instead.
The text was updated successfully, but these errors were encountered: