材料样式未在生产中加载

发布于 2025-01-28 20:03:18 字数 1259 浏览 2 评论 0原文

我目前正在从事一个带有NextJ和材料UI的项目。在开发模式下一切都很好。当我构建项目时,第一页加载正常,但是当我导航到第二页时,许多组件加载而无需造型,例如对话框。

在开发模式下:

“在此处输入图像描述”

在生产构建中:

“在此处输入图像说明”

我猜想这是情感cache无法正确注入SSR的问题,或者与我的方式注入使用链接组件:

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

如此示例一样实现情感缓存: https://github.com/mui/material-ui/tree/master/master/examples/nextjs-with-typescript

react版本:18.1.0(我已经尝试了版本17.0.2和18.0 AS出色地) 下一步:12.1.6 MUI:5.7

这个问题建议使用serverstylesheets,该片MUI5。

还有其他人经历过同样的问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独自←快乐 2025-02-04 20:03:18

我已经通过将这些软件包撞到最新版本来解决问题:

  • @emotion/cache
  • @emtion/react
  • @emotion/server
  • @emotion/stypled
  • @mui @mui
  • 下一个
  • react&amp;然后

,我在将项目与示例repo 发现我缺少这一点:

    "jsxImportSource": "@emotion/react",

我不确定这些动作中的哪一个实际上解决了问题,但我猜是后者。

如果您遇到类似问题,则可以尝试将其添加到Tsconfig中,看看它是否修复了问题。

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.

余罪 2025-02-04 20:03:18

尝试更新这样的_document文件:

您可以使用默认的_个文件文件,只需添加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()],
  };
};

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()],
  };
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文