【发布时间】: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),应用于导航栏项目(<Box height="70" alignItems="center" justifyContent="ce....)的 css 应用于围绕我的图像的框。这只是生产中发生的问题之一,只是为了展示问题。所有的风格都很奇怪,而且在产品中坏掉了。
在 gatsby-image 周围的 Div 上的 CSS(应该没有样式)
我尝试过的:
- 删除了
gatsby-plugin-offline(这似乎会导致问题,反正atm不需要它) - 重新排序各种页面上的组件
- 已删除
gatsby-plugin-emotion(无更改) - 删除
.cachenode_modules和package-lock.json并重新安装软件包 - 更新了所有包
- 用 render 替换 rehydrate 函数(这确实破坏了更多东西)
- 阅读了一堆相关的 gitlab 问题,其中大多建议删除
gatsby-offline-plugin、清除缓存和更新包。
此处提供了显示问题的示例:https://github.com/Console32/BrokenCss
【问题讨论】:
-
我看到您当前的cloundfront site 工作正常吗?
-
@ShinaBR2 不,不是,它在第二次渲染时中断,如果您导航到任何其他站点并返回主页,您会看到它应该是什么样子
-
你在使用 gatsby 风格的组件插件吗?
-
@claudios no,你可以在这里查看配置文件github.com/Console32/BrokenCss/blob/master/gatsby-config.js
标签: css material-ui gatsby