'Material styling not loading in production build

I'm currently working on a project with Nextjs and Material UI. Everything works great in development mode. When I build the project, the first page loads just fine but when I navigate to a second page, a lot of the components load without styling, such as a Dialog.

In dev mode:

enter image description here

In the production build:

enter image description here

I'm guessing it's either a problem with the emotion cache not being injected properly with SSR or with the way I'm using the Link component:

    //Link from next/link
    <Link href={href}>
      <ButtonBase sx={{ width: "100%" }}>
        <CardContent item={item} />
      </ButtonBase>
    </Link

The emotion cache is implemented like this example: https://github.com/mui/material-ui/tree/master/examples/nextjs-with-typescript

React version: 18.1.0 (I've tried with versions 17.0.2 and 18.0 as well) Next: 12.1.6 MUI: 5.7

This question suggests using ServerStyleSheets, which is not included in MUI5.

Has anyone else experienced the same issue?



Solution 1:[1]

I've managed to solve the issue myself by first bumping these packages to their latest versions:

  • @emotion/cache
  • @emtion/react
  • @emotion/server
  • @emotion/styled
  • @mui
  • Next
  • React & React-dom

Then I added a property to tsconfig.json after thoroughly comparing my project with the example repo and finding out I was missing this:

    "jsxImportSource": "@emotion/react",

I'm not entirely sure which one of these actions actually solved the issue but I'm guessing the latter.

If you're running into a similar problem, you could try adding it to your tsconfig and see if it fixes things.

Solution 2:[2]

Try update the _document file like this:

You can use the default _document file just add the getInitialProps part

import React from 'react';
import Document, {
  Html, Head, Main, NextScript,
} from 'next/document';
import { ServerStyleSheets } from '@mui/styles'; // works with @material-ui/core/styles, if you prefer to use it.

export default class MyDocument extends Document {
  render() {
    return (
      <Html lang="en">
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}

// `getInitialProps` belongs to `_document` (instead of `_app`),
// it's compatible with server-side generation (SSG).
MyDocument.getInitialProps = async (ctx) => {
  // Resolution order
  //
  // On the server:
  // 1. app.getInitialProps
  // 2. page.getInitialProps
  // 3. document.getInitialProps
  // 4. app.render
  // 5. page.render
  // 6. document.render
  //
  // On the server with error:
  // 1. document.getInitialProps
  // 2. app.render
  // 3. page.render
  // 4. document.render
  //
  // On the client
  // 1. app.getInitialProps
  // 2. page.getInitialProps
  // 3. app.render
  // 4. page.render

  // Render app and page and get the context of the page with collected side effects.
  const sheets = new ServerStyleSheets();
  const originalRenderPage = ctx.renderPage;

  ctx.renderPage = () => originalRenderPage({
    enhanceApp: (App) => (props) => sheets.collect(<App {...props} />),
  });

  const initialProps = await Document.getInitialProps(ctx);

  return {
    ...initialProps,
    // Styles fragment is rendered after the app and page rendering finish.
    styles: [...React.Children.toArray(initialProps.styles), sheets.getStyleElement()],
  };
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel de Jong
Solution 2 Mircea Matei