【问题标题】:css breaks in production of Gatsby, MaterialUI盖茨比、Material UI 制作中的 css 中断
【发布时间】:2021-01-02 21:55:43
【问题描述】:

我有一个使用 MaterialUI 组件的 gatsby 网站。

不知何故,css 样式被应用于我网站的错误组件。我得到了与问题相关的以下代码。

布局.js

    <ThemeProvider theme={theme}>
      <CssBaseline/>
      <Header onMenu={() => setMenuOpen(true)}/>
      {children}
    </ThemeProvider

Header.js

const NavigationBar = ({onMenu}) => {
  const trigger = useScrollTrigger({target: (typeof window !== `undefined` ? window : undefined)})
  const data = useStaticQuery(query)
  return (
    <React.Fragment>
      <Slide appear={false} direction="down" in={!trigger}>
        <AppBar color={"primary"}>
          <Toolbar disableGutters={true}>
           ...
            <LaptopAndWider>
              {data.dataJson.navigationPrimary.map(x =>
                <Button key={x.title} component={Link} to={x.path} color="inherit" underline={"none"}>
                  <Box height="70" alignItems="center" justifyContent="center" display="flex"> // This styles (height, flexBox) gets applied to the wrong item
                    <Typography variant={"h6"}>
                      {x.title}
                    </Typography>
                  </Box>
                </Button>
              )}
              {data.dataJson.navigationSecondary.map(x =>
                <Button key={x.title} component={Link} to={x.path} color="inherit" underline={"none"}>
                  <Box height="70px" alignItems="center" justifyContent="center" display="flex">
                    <Typography variant={"body1"}>
                      {x.title}
                    </Typography>
                  </Box>
                </Button>
              )}
            </LaptopAndWider>
           ...
          </Toolbar>
        </AppBar>
      </Slide>
      <Box height={82}/>
    </React.Fragment>
  );
}

还有下面的index.js

const IndexPage = ({data}) => (
  <React.Fragment>
    <Box> // Gets applied to this Box
      <GatsbyImage fluid={data.file.childImageSharp.fluid}
                   />
    </Box>
    ...
  </React.Fragment>
)

我在 gatsby 中使用了以下可能相关的插件:

  plugins: [
    ...
    `gatsby-theme-material-ui`,
    `gatsby-plugin-emotion`,
    {
      resolve: `gatsby-plugin-layout`,
      options: {
        component: require.resolve(`./src/components/Layout`),
      },
    }
  ],

当我使用 gatsby 开发时,jss/css 按预期工作。但在生产中(gatsby build && gatsby serve),应用于导航栏项目(&lt;Box height="70" alignItems="center" justifyContent="ce....)的 css 应用于围绕我的图像的框。这只是生产中发生的问题之一,只是为了展示问题。所有的风格都很奇怪,而且在产品中坏掉了。

导航栏项上的 CSS

在 gatsby-image 周围的 Div 上的 CSS(应该没有样式)

我尝试过的:

  • 删除了gatsby-plugin-offline(这似乎会导致问题,反正atm不需要它)
  • 重新排序各种页面上的组件
  • 已删除 gatsby-plugin-emotion(无更改)
  • 删除 .cache node_modulespackage-lock.json 并重新安装软件包
  • 更新了所有包
  • 用 render 替换 rehydrate 函数(这确实破坏了更多东西)
  • 阅读了一堆相关的 gitlab 问题,其中大多建议删除 gatsby-offline-plugin、清除缓存和更新包。

此处提供了显示问题的示例:https://github.com/Console32/BrokenCss

【问题讨论】:

标签: css material-ui gatsby


【解决方案1】:

与 CSS 类名的冲突是由 multitree setup in your application 引起的。

您可以通过提供用于生成类的类名生成器来解决此冲突。这样 Gatsby 和 MDX 将使用相同的类名生成器来生成类名。

安装react-jss

yarn add react-jss

src/components/Layout.js中,提供相同的类名生成器,

import React from 'react';
//...
import {createGenerateId, JssProvider} from 'react-jss';

const generateId = createGenerateId();

const Layout = ({children}) => {  
  //...
  return ( 
    <JssProvider generateId={generateId}>
      <ThemeProvider theme={theme}>
      {/* ... */}
      </ThemeProvider>
    </JssProvider>
  );
}

【讨论】:

  • 感谢您的建议,遗憾的是,即使我指定了 ID,问题仍然存在(我已在演示存储库中添加了一个分支,其中包含您的建议 here
  • 从演示项目中完全删除 mdx 也没有改变任何东西。
【解决方案2】:

问题源于LaptopAndWider 组件。该组件使用react-responsive 中的MediaQuery 来实现在不同屏幕尺寸上隐藏内容。因此,SSR 从未在 LaptopAndWider 下方渲染 Material UI 组件,这导致样式缺失。 CSR 确实有效,这就是为什么在来回导航应用正确样式之后的原因。

用来自@material-ui/coreHidden 替换来自react-responsiveMediaQuery 可以解决问题。

            <Hidden mdDown>
              {data.dataJson.navigationPrimary.map(x =>
                <Button key={x.title} component={Link} to={x.path} color="inherit" underline={"none"}>
                  <Box height="70" alignItems="center" justifyContent="center" display="flex"> // This styles (height, flexBox) gets applied to the wrong item
                    <Typography variant={"h6"}>
                      {x.title}
                    </Typography>
                  </Box>
                </Button>
              )}
              {data.dataJson.navigationSecondary.map(x =>
                <Button key={x.title} component={Link} to={x.path} color="inherit" underline={"none"}>
                  <Box height="70px" alignItems="center" justifyContent="center" display="flex">
                    <Typography variant={"body1"}>
                      {x.title}
                    </Typography>
                  </Box>
                </Button>
              )}
            </Hidden>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 2020-10-16
    • 2019-08-07
    • 1970-01-01
    • 2021-12-14
    相关资源
    最近更新 更多