Issue with initial loading of Nextjs Material UI CSS causing display problems

Utilizing Next.js in conjunction with Material UI, the Material UI CSS does not load correctly upon initial page load, resulting in a display error similar to this image: https://i.sstatic.net/7wpxM.png

The correct styling is achieved after a page transition like so: https://i.sstatic.net/HdIzJ.png

(There are many other instances where elements such as fixed position headers are out of place.)

However, transitioning from one page to another or triggering a setState command resolves the issue and applies the CSS properly. This discrepancy may be due to a class name mismatch between server and client despite implementing _app and _document from this link. The problem still persists.

Therefore, my only recourse is to include a Twitter or Instagram-like loading screen on the first visit, which then rectifies the styling once some state is set. How can I detect in Next.js if it's a user's first visit without resorting to using a context provider that would require adding a consumer to every page?

Is there any workaround or solution for this longstanding issue? I have reached out to Material UI creators who indicated that an upcoming upgrade will address this, but until then, what steps should I take?

Answer №1

It took me days to realize that a small change in my _document.js file made all the difference.

import { ServerStyleSheets } from "@material-ui/styles";

I made the switch to:

import { ServerStyleSheets } from "@material-ui/core/styles";

And just like that, the CSS was applied flawlessly and the annoying flickering issues disappeared.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The visibility of the eye icon on the password field may vary, occasionally appearing and disappearing from view

Here is the code for my login form password field (I'm using bootstrap 4): <div class="form-account-label-input"> <label> Password <span class="star-red">&nbsp;*</span></label> <input type ...

Animating the first element using Semantic UI React Transition Group

Is there a way to modify Semantic's Transition.Group example (found at ) to animate the topmost element instead of the bottommost one? In the example provided, pressing the add or subtract button results in the animation applying only to the last ele ...

Ruby on Rails slider bar functionality

Currently, I am developing a video-focused application using Ruby 1.9.2 and Rails 3.1. One of the key features I need to implement is a slider bar that allows users to adjust the total duration of a video in real-time. Despite my attempts to use HTML5 fo ...

Could not locate module: The package path ./react is not exported from the package in E:NextAppportfolio_website-mainportfolio_website-main ode_modules ext-auth

I am encountering an issue while trying to import SessionProvider from Next-Auth. The error message that is being displayed is: "Module not found: Package path ./react is not exported from package E:\NextApp\portfolio_website-main\port ...

The antd module's attempt at exporting the default value using module.exports = exports['default'] is failing to function as

After implementing antd's suggestions, I have integrated babel-plugin-import and included the following babel loader in my webpack configuration. webpack.config { test: /\.js?$/, loader: 'babel-loader', exclude: /node_modules/, ...

Obtain the API by navigating through nested dynamic routes in Next.js

Can someone help me with the folder structure in NEXT JS for pages/api/product/[id]/index.js? What would be the URL to access the Api index in the [id] folder? Here is my folder structure pages/ api/ product/ [id]/ index.js I nee ...

Displaying live updating information (Progress) on my website

I'm in the process of creating a web page that displays data from a datatable in a grid view. There's also a button labeled "Send To Excel." When a user clicks this button, the program begins generating a report by writing the datatable content i ...

Safari: Height of 100% not functioning as expected

Currently, I have a div with the class of "signup-login-forms" that is set to have a height of 40vh and span 100% of the browser width. Inside this div are two forms positioned next to each other. The goal is to have both forms fill the entire height of th ...

Applying drop cap styles to generated content in pseudo-elements

In my blogging Q&A section, I wanted to add a unique touch by starting each question with a large question mark and each answer with a large exclamation mark as a drop cap. I tried using the ::first-letter pseudo-element along with the initial-letter prope ...

What is the best way to resize a PDF to match the width and height of a canvas using

I need help with a project involving rendering previews of PDF documents in a UI similar to Google Docs. {documents.map((doc) => { return ( <div key={doc.id} className=" ...

Problem with uploading to npm registry

I've been working on an exciting Open Source project using React and I'm facing a challenge with pushing it to npm. The problem is, my components work perfectly in a test environment when mounted to other components, but once I publish the packag ...

Adjust the text placement following its entry on the screen

I'm currently facing an issue with my code that involves text sliding in and then moving to the left after the slide completes. How can I ensure that the text remains in place once the slide is finished? <style> div.slide-left { width: ...

The event handling mechanism in React JS for elements coming into view

Can someone please guide me on implementing the inview event in React JS? I want to achieve something like this: <div inview={handleInView}></div> I specifically need to add it to the Footer section so I can dynamically load more news a ...

Contrasting the disabled class of Bootstrap with the disabled attribute: a deeper dive into

Hey there, I've been using both the disabled class and the disabled attribute, but I'm not sure about the difference between the two. Can someone clarify when to use the class and when to use the attribute? I have a button in my code that I want ...

What is the solution to the error message "java.lang.NoSuchMethodError: No static method weight$default in JetpackCompose"?

After attempting to develop a wheel date picker in compose using built-in libraries without success, I opted to utilize a third-party library called Wheel date and time picker. However, upon running it, I encountered an error specifically on Android 5.1, w ...

Using column-count in Opera with max-height

Encountering an issue in Opera 12 when attempting to combine column-count with max-height. The problem arises when Opera generates more columns than specified in column-count when a fixed height is set, instead of adding a scroll option. For example, cons ...

Troubleshooting a mapping problem with CSS elements

While building my website, I have successfully mapped the elements by ID for all alphabet letters except A. When clicking on the letter A, it does not go to the respective elements. Can anyone please help me find a solution? Visit this link for reference ...

Adaptive text sizing within Microsoft Outlook

I am facing a challenge in making the font size of my email signature responsive. Although VW seems to be the solution, it doesn't seem to work in Outlook. I have attempted using CSS, but it appears that inline CSS might be necessary. However, I am un ...

Setting up a ClientBundle in GWT with multiple CssResources

I am currently in the process of updating some older code to GWT 2 and encountering some strange behavior. I have a custom interface that extends ClientBundle, as recommended by the GWT documentation. Within this bundle, I have defined multiple CssResource ...

Personalized renderInput within the material-ui autocomplete

Is there a way to display my Account component in both renderInput and renderOptions? I need it to show up in both scenarios. demo.js /* eslint-disable no-use-before-define */ import React from "react"; import Autocomplete from "@material-u ...