-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
production build - classnames conflicts #8223
Comments
I have already seen some issue around this problem. It was always linked to duplicated className generator instances. I can't help more without a reproduction. |
I'm closing the issue for now as not actionable. Let me know if you have a reproduction example. |
@oliviertassinari I was able to reproduce the problem. Here is the webpack bin - https://www.webpackbin.com/bins/-KuO6ia3h-JDpBOJncZ3 In my case, I have 2 application roots which are mounted independently to 2 different div. Both use the same material-ui Based on
And my screenshot confirms that for some reason the counter is duplicated. I need help. I don't know what I'm doing wrong right now. |
@darkowic You need to share the jss instance between the different react trees. You should be good with such change. |
@oliviertassinari I think I'm doing it using my custom
I wrap my every react tree with this. |
This issue should be opened. |
Sure, let's sum up what we know so far. This issue arise as soon as you are using multiple react rendering tree. The jss provider is going to create two class name generators, one for each tree. Hence we end up with class name conflicts. |
WORKAROUND for client-side application: You can create your own import warning from 'warning';
// Returns a function which generates unique class names based on counters.
// When new generator function is created, rule counter is reset.
// We need to reset the rule counter for SSR for each request.
//
// It's an improved version of
// https://github.com/cssinjs/jss/blob/4e6a05dd3f7b6572fdd3ab216861d9e446c20331/src/utils/createGenerateClassName.js
//
// Copied from material-ui due to issue https://github.com/callemall/material-ui/issues/8223
// This counter is moved outside from `createGenerateClassName` to solve the issue
let ruleCounter = 0;
export default function createGenerateClassName() {
return (rule, sheet) => {
ruleCounter += 1;
warning(
ruleCounter < 1e10,
[
'Material-UI: you might have a memory leak.',
'The ruleCounter is not supposed to grow that much.'
].join('')
);
if (process.env.NODE_ENV === 'production') {
return `c${ruleCounter}`;
}
if (sheet && sheet.options.meta) {
return `${sheet.options.meta}-${rule.key}-${ruleCounter}`;
}
return `${rule.key}-${ruleCounter}`;
};
} |
Nice one, we had such a use case on the server and I was already planing to for a bold hint in the documentation see #5 But now you actually found a good reason to support 2 parallel running providers. |
@kof You have just found a use case for a multiple parallel JssProviders on the client side. And I think the proposed solution is simple to implement :). |
This will mean that on the server, ruleCounter will never get reseted. We can't do that. |
On the server side, the JssProviders definitely needs to support concurrent async rendering of a react tree (strong need). But the current implementation makes it already supported :) |
@darkowic Proposed a workaround having no access to the underlying stack. We do. We can do better with this extra flexibility: accepting a class name generator instance. |
Also request to the same endpoint should always respond with same class names. |
import {createGenerateClassName} from 'react-jss'
const generateClassName = createGenerateClassName()
<JssProvider generateClassName={generateClassName}>
<App1 />
</JssProvider>
<JssProvider generateClassName={generateClassName}>
<App2 />
</JssProvider>
<JssProvider classNamePrefix="app-1">
<App1 />
</JssProvider>
<JssProvider classNamePrefix="app-2">
<App2 />
</JssProvider> Pro 1:
Pro 2:
|
Actually it makes sense to have both props |
I've ran into this issue via facebook/create-react-app#3173 (and the linked reduced test case). In my case, a material UI-dependant component (v1.0.0-beta.11) was included into an app that also uses material-ui (same versions). In development, this works fine, but in production the layout is broken due to conflicting class names. I'm not sure if this qualifies as "multiple react rendering trees" and moving |
Created a PR which should fix this in react-jss https://github.com/cssinjs/react-jss/pull/155 |
So let's sum up.
|
@w3nda This is too generic, there is a million different reason for this problem to happen.
We might revert the classname hashing logic. While it greatly improves the performance on the server, it has a significant cost on the client. Maybe we will provide a flag to enable it instead. So no, I think that the best option is to solve the core issue. Did you try this page https://material-ui.com/guides/server-rendering/#troubleshooting? |
@oliviertassinari Thanks for the fast response, I really don't know how to debug the problem and the link you mentioned isn't relevant to me because I serve it as a static site.. |
@w3nda Static website has the same issue. You need to generate the HTML, so it will use the server-side rendering API. If the index counter leaks between two pages, the class name won't be correct. Well, I think that a slower class name generator is a good tradeoff compared to the pain it is to debug such a problem (& how frequent it is). So, yes, you can upgrade to |
I just had a similar problem and it was an old material-ui import that was in one of our libraries. It worked fine in development mode but was broken in production. I'm pretty sure this was working before, I don't know if a warning could be issued in this case, even if it's clearly our fault. |
Hi, I am using material just for a single input, button and form on my site and I had to use a It's annoying to need this jss provider, is there another fix we can do that would not increase the final build size? |
@kopax What are you using |
Hi @oliviertassinari, In In Something similar here happens with a form (weird box shadows), we need to visit another page to have the proper css, that happens only in production: Both issues are fixed if we add |
I have no idea. I can't help with the provided information. |
Everything is broken too. I observe wrong order Can't find the reason yet. |
@oliviertassinari perhaps those version number used by [email protected] we are using would help you tell us what's going on: "@material-ui/core": "^1.4.0",
"@material-ui/icons": "^1.0.0",
"material-ui-chip-input": "1.0.0-beta.6 - 1.0.0-beta.8", |
Could you make sure that Material-UI is only bundled once? Using two versions at the same time can create the same buggy behavior. |
Well. It's possible. We're using I solved the bug on production by adding import React from "react";
import { Admin, Resource } from "react-admin";
import JssProvider from "react-jss/lib/JssProvider";
const App = () => (
<JssProvider>
<Admin dataProvider={dataProvider}>
<Resource name="trip" list={RequestsList} className="resourceItem" />
</Admin>
</JssProvider>
);
export default App; |
@andrewkslv this is exactly what we are trying to avoid, we'd prefer to use it without @oliviertassinari I've just checked and I did have some dependencies issues. I fixed it but still got the errors with the following ├─┬ @bootstrap-styled/[email protected]
│ └── @material-ui/[email protected]
├── @material-ui/[email protected]
└─┬ [email protected]
└── @material-ui/[email protected] After doing: rm -rf node_modules/@bootstrap-styled/ra-ui/node_modules/@material-ui/
rm -rf node_modules/ra-ui-materialui/node_modules/@material-ui/
npm ls @material-ui/core
├─┬ @bootstrap-styled/[email protected]
│ └── UNMET DEPENDENCY @material-ui/[email protected]
├── @material-ui/[email protected]
└─┬ [email protected]
└── UNMET DEPENDENCY @material-ui/[email protected] Then now it does work (no css issue in production). I guess this is not what I want... Related project @material-ui dependencies: Any idea what to do? |
@kopax It's hard to tell without something I can debug. I'm happy to hear it's working now. I have noticed you are using styled-components with Material-UI. If you have some time, I would love to chat about the integration on Gitter. |
The working solution isn't natural. Which means it involve task that cannot be run with npm. I will not use it, i gave this as a hint. We'll have the chance on monday, I'll join the mui gitter channel. |
Hi @kopax, did you manage to find a solution? |
No not yet. Not without the provider. @oliviertassinari I am on gitter. |
@andrewkslv Your solution really worked for me. I'm also using |
Same issue here. Why adding JssProvider helps? |
I have added a warning in v4 to warn when duplicated style instances are used: #15422. |
I don't know. I raised this issue in |
Where i can find the solution about production build - classnames conflicts #8223 ? Thanks, |
@oliviertassinari Facing this issue again, even though i have followed all the instructions. Since it is working for others, i guess i might be missing something basic. I'm using following versions of the packages. "@material-ui/core": "^4.6.1", Update: Issue got resolved. Sorry for not going through documentation thoroughly. This part of the documentation solved my issue. https://material-ui.com/styles/api/#creategenerateclassname-options-class-name-generator But somehow, the JSSProvider solution which was working for all others, was not working for me. Anyways, thank you :) |
Thanks @KannugoPrithvi , this is the good solution ! https://material-ui.com/styles/api/#creategenerateclassname-options-class-name-generator |
The css class names definitions are duplicated for some components - in my case it is duplicated (I guess from my debugging) for
MuiIconButton
andMuiModal
- check current behaviorExpected Behavior
The class names should not be duplicated across components.
Current Behavior
My button styles:
The class is duplicated.
Styles definition:
It is working in development mode:
My buttons styles:
and found the definitions:
and from modal:
Steps to Reproduce (for bugs)
I can try to prepare the environment to reproduce the problem but right now I just wanted to report it here.
Context
I'm trying to release my application with the production environment.
Your Environment
I need some hints where may be the problem. I'm not using
withStyles
solution anywhere - I use styled components for styles overriding.The text was updated successfully, but these errors were encountered: