Skip to content
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

makeSyles css injection order difference between dev and prod #6529

Closed
smeng9 opened this issue Aug 24, 2021 · 3 comments
Closed

makeSyles css injection order difference between dev and prod #6529

smeng9 opened this issue Aug 24, 2021 · 3 comments

Comments

@smeng9
Copy link
Contributor

smeng9 commented Aug 24, 2021

What you were expecting:

I would expect the style sheets injection between <head> and </head> would follow the same order in production as in development.
The correct order of style sheets should be Material UI(as it appears first it has lowest priority), React Admin, makeStyles created by ourselves to override(has highest priority)

correctbehavior

What happened instead:

The style sheets injection order in prod is mixed, which creates a lot of styling issues for my project.
Some of our components display well in the development environment, but got messed up in production.

cssinjectionorder

Although some of the order flip in style sheet may not be noticeable, some will causes issues because of the generated order.

dev:

correctstyle

prod:

incorrect

Steps to reproduce:

You can also easily reproduce the behavior by going to your demo website https://marmelab.com/react-admin-demo/#/
Right click to inspect element and check the order of style sheets there

It will also look like a mixed order.
messedupinjection

Related code:

Inspect element of dev mode in codesandbox below in the iframe and the order is correct:

https://codesandbox.io/s/github/marmelab/react-admin/tree/master/examples/demo

Other information:

I have searched some related past issues, not sure if they explained clear enough to get it resolved.
#4388 (comment)
#5428 (comment)

Addition Remarks:
Material-UI v5 will deprecate makeStyles, withStyles infavor of styled()
However the same issue may persist if we don't understand the root cause when updating to react admin 4.0 #5933
I think this should be added to 4.0 wishlist because the order of stylesheets really matters

Environment

  • React-admin version: 3.17.2
  • Last version that did not exhibit the issue (if applicable): None
  • React version: 17.0.2
  • Browser: Firefox 91
  • Stack trace (in case of a JS error): None, CSS error
  • Production version is compiled using: create-react-app 4.0.3 with no change on configuration
@fzaninotto
Copy link
Member

fzaninotto commented Aug 25, 2021

Hi, and thanks for your report.

I think your main assumption is wrong:

The correct order of style sheets should be Material UI(as it appears first it has lowest priority), React Admin, makeStyles created by ourselves to override(has highest priority)

According to material-ui's styling documentation, the order is different:

The injection of style tags happens in the same order as the makeStyles / withStyles / styled invocations.

So I think there is no material-ui or react-admin bug in your situation - just a misunderstanding of the injection rules.

As for having different styles in development and production, this is a recurrent problem with material-ui (see e.g. #1782). Our recommendation is to make sure you don't have duplicate instances of material-ui packages in your lock files, wipe and reinstall dependencies with a clean resolution.

@panfiva
Copy link

panfiva commented Sep 1, 2021

i had a similar issue with styles not applied in the past; the issue was a circular dependency in my import statements

See #4370 for an issue i related in react-admin; however it was fully resolved at mui/material-ui#19489

@smeng9
Copy link
Contributor Author

smeng9 commented Sep 1, 2021

Hi all,

After further investigation, the bug is neither from material-ui nor react-admin. I also double checked there are no duplicate instances of material-ui.

The actual issue is from the optimization/tree-shaking in the webpack config file of create-react-app. webpack/webpack#7094
The optimization introduced by them messes up the stylesheet order in production.
After disabling such optimization by setting webpack.config.js {mode: "development"} using react-app-rewired the production version behaves the same as development version.

https://stackoverflow.com/a/62646041 have provided one solution, but it is not applicable in this case, because we cannot add {index: 1} to the already compiled package of react-admin.

There are other solutions to wrap the component with such issues under another theme provider to force a customized theme (proposed on stackoverflow), however it does not solve the root cause.

@smeng9 smeng9 closed this as completed Sep 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants