material-ui icon indicating copy to clipboard operation
material-ui copied to clipboard

production build - classnames conflicts

Open darkowic opened this issue 9 years ago • 63 comments

The css class names definitions are duplicated for some components - in my case it is duplicated (I guess from my debugging) for MuiIconButton and MuiModal - check current behavior

  • [x] I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

The class names should not be duplicated across components.

Current Behavior

My button styles: classnames_conflict The class is duplicated. Styles definition: classnames_conflit_2

It is working in development mode: My buttons styles: development_class

and found the definitions: mui-icon-button-dev

and from modal: mui-modal-dev

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

Tech Version
Material-UI 1.0.0-beta.10
React 15.6.1
browser any
webpack ^3.3.0

I need some hints where may be the problem. I'm not using withStyles solution anywhere - I use styled components for styles overriding.

darkowic avatar Sep 15 '17 16:09 darkowic

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.

oliviertassinari avatar Sep 15 '17 17:09 oliviertassinari

I'm closing the issue for now as not actionable. Let me know if you have a reproduction example.

oliviertassinari avatar Sep 15 '17 23:09 oliviertassinari

@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 ThemeProvider wrapped with JssProvider to override insertionPoint from jss.

generate_classnames_counter

Based on createGenerateClassName function, you use counter to have unique class names

export default function createGenerateClassName(): generateClassName {
  let ruleCounter = 0;

  return (rule: Rule, sheet?: StyleSheet): string => {
    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}`;
  };
}

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 avatar Sep 19 '17 08:09 darkowic

@darkowic You need to share the jss instance between the different react trees. You should be good with such change.

oliviertassinari avatar Sep 19 '17 08:09 oliviertassinari

@oliviertassinari I think I'm doing it using my custom ThemeProvider

  <JssProvider registry={context.sheetsRegistry} jss={context.jss}>
    <MuiThemeProvider
      theme={context.theme}
      sheetManage={context.sheetsManager}
      {...props}
    />
  </JssProvider>

I wrap my every react tree with this.

darkowic avatar Sep 19 '17 09:09 darkowic

This issue should be opened.

darkowic avatar Sep 21 '17 07:09 darkowic

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. @kof Should we extend the JssProvider from react-jss to accept a class name generator?

oliviertassinari avatar Sep 21 '17 07:09 oliviertassinari

WORKAROUND for client-side application: You can create your own createGenerateClassName and move ruleCounter outside the wrapper function

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}`;
  };
}

darkowic avatar Sep 21 '17 07:09 darkowic

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 avatar Sep 21 '17 07:09 kof

We need to research if there are use cases for a strong need of multiple parallel JssProviders. If there are, we need to think of something to support it.

@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 :).

oliviertassinari avatar Sep 21 '17 07:09 oliviertassinari

move ruleCounter outside the wrapper function

This will mean that on the server, ruleCounter will never get reseted. We can't do that.

kof avatar Sep 21 '17 07:09 kof

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 :)

oliviertassinari avatar Sep 21 '17 07:09 oliviertassinari

@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.

oliviertassinari avatar Sep 21 '17 07:09 oliviertassinari

Also request to the same endpoint should always respond with same class names.

kof avatar Sep 21 '17 07:09 kof

@kof Should we extend the JssProvider from react-jss to accept a class name generator?

  1. Yeah one possible way would be to allow JssProvider accept a class name generator like this:

import {createGenerateClassName} from 'react-jss'

const generateClassName = createGenerateClassName()

<JssProvider generateClassName={generateClassName}>
  <App1 />
</JssProvider>

<JssProvider generateClassName={generateClassName}>
  <App2 />
</JssProvider>

  1. Another potential option would be to provide some prefix, like an application name. This could work if we assume we don't have unpredictable amount of applications.

<JssProvider classNamePrefix="app-1">
  <App1 />
</JssProvider>

<JssProvider classNamePrefix="app-2">
  <App2 />
</JssProvider>

Pro 1:

  • User doesn't need to provide the prefix

Pro 2:

  • User can have something meaningful in the DOM class names that identifies the app during development
  • User have more control about the prefix.
  • It is relatively easy to have a non-conflicting prefix. Either you have just a few subtrees and naming is already easy enough or you can add you own number prefix to those names and reset them on each request.

kof avatar Sep 21 '17 07:09 kof

Actually it makes sense to have both props classNamePrefix and generateClassName. First for debugging, second for automated uniqueness of classnames.

kof avatar Sep 22 '17 10:09 kof

I've ran into this issue via https://github.com/facebookincubator/create-react-app/issues/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 var ruleCounter = 0; before createGenerateClassName() did not workaround the issue, but commenting out the following, did work:

https://github.com/callemall/material-ui/blob/2361339fd6df9bfbb85ed2ee4da9d42ee6fee71c/src/styles/createGenerateClassName.js#L26-L28

tlvince avatar Sep 22 '17 17:09 tlvince

Sorry, I couldn't provide more info at the time I opened #7855.😊 This comment basically covers the issue I faced when running production build.

A workaround for this in the pipeline?

robophil avatar Sep 22 '17 23:09 robophil

Created a PR which should fix this in react-jss https://github.com/cssinjs/react-jss/pull/155

kof avatar Sep 23 '17 08:09 kof

So let's sum up.

  • @darkowic's issues root cause is described here: https://github.com/callemall/material-ui/issues/8223#issuecomment-331076580 and will be fixed thanks to @kof's PR: cssinjs/react-jss#155 and a new warning will be added to prevent this situation in the future: #8341.

  • @tlvince's issue symptoms are the same, but the root cause is different. You are using a single react render tree. It could be linked to a duplicated Material-UI version installed. Check your warnings and if you can figure out exactly what's going on, please open another issue.

  • @Robophil's issue is too vague to be actionable.

oliviertassinari avatar Sep 23 '17 10:09 oliviertassinari