ServerStyleSheets does not propagate props to StylesProvider component.
See original GitHub issue- The issue is present in the latest release.
- I have searched the issues of this repository and believe that this is not a duplicate.
Current Behavior 😯
I’m trying to add Material-UI to my application in Next.js. I started from checking out example that you have in your official docs: https://github.com/mui-org/material-ui/tree/master/examples/nextjs
I wanted to change injection order of CSS - in my application, components will be styled with SCSS files so I wanted those stylesheets to override jss stylesheets. According to documentation passing an object to ServerStyleSheets spreads that object to StylesProvider component: https://github.com/mui-org/material-ui/tree/master/examples/nextjs so I passed injectFirst
prop, but it doesn’t get passed to StylesProvider. Other props, like disableGeneration
do get passed and they work, but injectFirst
doesn’t.
Expected Behavior 🤔
Passing injectFirst
prop to ServerStyleSheets
should pass it to StylesProvider
which should result in changed injection order of CSS files.
Steps to Reproduce 🕹
Link to codesandbox: https://codesandbox.io/s/github/mui-org/material-ui/tree/master/examples/nextjs
Steps:
- Add object
{ injectFirst: true }
as parameter toServerStylesSheets()
in_document.js
- Check order of stylesheets in
<head>
tag
Context 🔦
It’s impossible to have both SSR and changed injection order - injectFirst
prop doesn’t work, so Material-UI stylesheets always get injected last, thus overriding all my custom SCSS styles.
Your Environment 🌎
Tech | Version |
---|---|
Material-UI | v4.9.14 |
React | 16.13.1 |
Browser | Chrome |
TypeScript | x |
etc. | Next.js @ 9.4.0 |
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:11 (4 by maintainers)
Top GitHub Comments
Thank you. Problem solved. Nextjs always inserts the content of styles prop at the end of
<head>
element, but global / module css at the start. So I need to pass my own prop and render style tag.Working example:
I’m talking about changing styles order at build level (SSR).
<StylesProvider injectFirst/>
doesn’t work with SSR (I understand why). And I haven’t found a way to change styles injection order in _document.js at nextjs. I can’t hook into nextjs rendering. Only enhance app component with something like this:But this cause FOUC (it takes a little bit of time until JS loads and removes #jss-server-side - hence the linked workaround):
I dont’ want to use !import in my css files, that’s ugly.
Or am I missing something?