-
-
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
Styles not applied in first loading correctly with NextJS #30410
Comments
@shorcy Can you try the new It is something that we fix internally but how to configure correctly with NextJS. |
That fixed the issue. Thanks a lot, @siriwatknp :) |
@siriwatknp it doesn't work for me. I just copied everything from example: _app.js, document.js , I runed codemod to convert v4 JSS to MUI v5 styles. |
Getting the same issue. Then I tried setting |
Hi @siriwatknp I am experiencing the same issue. I followed the repo provided by MUI (https://github.com/mui/material-ui/tree/HEAD/examples/nextjs-with-typescript). The theme is applied in local dev but not when I deploy to production. (https://btdtech.io/) As you can see from the website, when you first visit it there is no styling applied. If you use the link to navigate to the ABOUT page then you will see it styled. However if you refresh the ABOUT page, the styling disappears again. This is the link to the repository for your perusal https://github.com/btdtech/company-website. Look forward to your reply! Bac |
Hi everyone, I have been digging and it seems that Emotion cache's style component has to style. Do we know why this is? I really would like to get to the bottom of this, otherwise I might have to find another CSS solution. Cheers, Bac |
I can confirm that our official example does not work. I looking into this. |
It seems like the problem comes from this: // pages/_document.js
const initialProps = await Document.getInitialProps(ctx);
// console.log(initialProps.html) returns empty string
const emotionStyles = extractCriticalToChunks(initialProps.html); The
|
I can confirm that the issue comes from nextjs -"next": "latest",
+"next": "canary", Change the package json then run Note that the latest nextjs (v12.1.6) (that does not work) was published 28 days ago and I guess this PR vercel/next.js#36792 fixes the issue. |
Thank you so much @siriwatknp . It solved the issue! I can understand that there is a bug in nextjs, but why was yarn important in all of this? I tried NPM but didn't work. |
Thanks, I am closing this issue then. |
@siriwatknp I downgraded MUI to 4 :-( |
@siriwatknp next.config: ( flag emotion )
_document: ( no extra enhance code and injections )
_app: ( no cache provider )
versions:
|
@Taliss The difference is that the example uses the advanced approach, not the default approach. |
I'm still facing this issue. nothing seems to work for me mentioned above. ReBank this is the website, I can't give the source code since its enterprise application but I can tell you that I'm using Next.js, MUI, styled-components (not @mui/styled-engine-sc, regular mui configuration with just styled-components styled api). As you can see styles are flickering on the first load. I'm using Next.js SWC and have styled-components turned on in next.config.js and using this configuration. Does anyone have this problem and is there any fix to this? |
I fixed it with adopting styled-engine-sc with yarn resolutions and next webpack resolve alias |
Hi, I upgraded NextJS from v12.0.10 to v12.1.7-canary.39 and I'm still seeing this issue, the style from the theme won't load on the page's first render, anyone can help? |
Working version as of today
|
Still facing the same problem, style is not load on first load, I also followed the _document.js. What else am i missing ?
|
Yup... Just wasted some time with this one. In case this is anyone else's issue. https://developer.chrome.com/blog/new-in-devtools-102/#network-preview I used to be able to click on the dev tools preview tab, and see a nicely rendered page. At some point I've presumably upgraded Chrome (or its done it itself 🤷♂️) and things stopped working. I probably never noticed at the time; so when I next clicked on the preview tab I got the whole 'plain html' with no CSS applied. (and assumed I'd broken something) I've just turned JavaScript via dev tools and checked; and everything is properly rendering. TLDR; things might actually be working / don't trust the chrome preview tab. Edit: Not 100% sure CSP applies to inline stylesheets; but I can see the styles in the response so 🤷♂️ |
I followed @Taliss 's comment in this thread: #30410 (comment) Works like a charm. Is there a benefit to using the default approach that is missing with this configuration? |
Adding this to _document.js finally solved the problem const sheets = new ServerStyleSheets(); ctx.renderPage = () => const initialProps = await Document.getInitialProps(ctx); return { |
It seems the example repo is now working. I just changed my I am testing on Brave Browser (which is just a layer on top of Chrome) version 1.43.93 (with Chromium: 105.0.5195.127) and with the following env:
|
I did what daveteu posted above and I still see the same issue happening but not as often. I have: Can anyone help please? |
Complete the v5 migration with tss-react, it worked for me. |
Hey Using @mui v5 my _document file is export default class MyDocument extends Document { render() { ); } } // // Render app and page and get the context of the page with collected side effects. ctx.renderPage = () => originalRenderPage({ const initialProps = await Document.getInitialProps(ctx); return { my_app file is export default function App({ Component,pageProps }: AppProps) { ` |
I followed @Taliss 's comment in this thread: #30410 (comment) Works like a charm. |
I had a similar problem but i worked around it probably with not the best solution. In my case i was making a project using react and django, this project had a google verification to login and after this it load the home page. In the first load only the buttons itens from material ui package were without the styled, but after you refresh the page the style works again. So i created a context to use a variable that apoint this first load and automatically refresh the page. Like i said, this is not the best way but worked for me. |
I have the above configuration work fine but get this issue after updating with the latest version 13.5.2, @hamza172 I tried your solution but now work in above version Thanks in advance if anyone can help with this |
so what is the right solution now in 2024? |
@siriwatknp I think it's an important issue. and now after more than 2 years, even with the new versions of Next js, MUI and Emotion, the problem still exists. can you look around please? |
I can confirm that not using
commenting |
#30410 (comment) |
My solution used suppressHydrationWarning and solved other details. It works this way:
Ps: Im using Next.js v14 and Material UI v5.16 (simplified code) Root Layout:
CustomThemeContext:
And no color-scheme related styles on any CSS file. |
still happening on Next 14.0.4 , |
Duplicates
Latest version
Current behavior 😯
When using SX prop I seem to be getting an issue when deploying the application where a lot of the styles are loaded after the dom has rendered. Even though I use the emotion cache in my _documents.tsx, and create and use the theme in _app.tsx.
Expected behavior 🤔
What should is that when rendering the page the first time all the style changes should already be there. but takes a small bit of time before it does.
https://imgur.com/a/ZujFrXd
Steps to reproduce 🕹
The issue is now only visible on the live site (Hosted with vercel).
https://daniiel.dev/
But for you want want to take a look or maybe have an idea what could cause it.
You can have a look at the repo and play around yourself by using the codesandbox bellow
codesandbox: https://codesandbox.io/s/hopeful-bush-5jbz8
Context 🔦
What I'm trying to accomplish is for my production build to look like my local development on initial load.
I think the issue lays in either the _documents.tsx or _app.tsx.
I have attached a link to the 2 images for how it looks in local development and production.
https://imgur.com/a/ZujFrXd
Your environment 🌎
Used both Firefox Developer Edition & Google Chrome (Version: 96.0.4664.110) for testing.
System:
OS: Windows 10 10.0.19044
Binaries:
Node: 16.13.1 - C:\Program Files\nodejs\node.EXE
Yarn: Not Found
npm: 8.1.2 - C:\Program Files\nodejs\npm.CMD
Browsers:
Chrome: Not Found
Edge: Spartan (44.19041.1266.0), Chromium (96.0.1054.62)
npmPackages:
@emotion/react: ^11.5.0 => 11.5.0
@emotion/styled: ^11.3.0 => 11.3.0
@mui/core: 5.0.0-alpha.53
@mui/lab: ^5.0.0-alpha.53 => 5.0.0-alpha.53
@mui/material: ^5.0.6 => 5.0.6
@mui/private-theming: 5.0.1
@mui/styled-engine: 5.0.2
@mui/system: 5.0.6
@mui/types: 7.0.0
@mui/utils: 5.0.1
@types/react: ^17.0.34 => 17.0.34
react: ^17.0.2 => 17.0.2
react-dom: ^17.0.2 => 17.0.2
typescript: ^4.3.5 => 4.3.5
--- tsconfig.json ---
{
"compilerOptions": {
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"target": "es5",
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": [
"next-env.d.ts",
"/*.ts",
"/*.tsx",
"src/components"
],
"exclude": [
"node_modules"
]
}
The text was updated successfully, but these errors were encountered: