【发布时间】:2020-05-15 10:44:23
【问题描述】:
我想知道为什么 Gatsby 在每个 html 页面的 head 标签中都包含未使用的 css。
我创建了一个新项目
gatsby new my-default-starter https://github.com/gatsbyjs/gatsby-starter-default
我添加了一个 css 模块样式的组件
.container {
margin: 3rem auto;
max-width: 600px;
border: dot-dot-dash;
}
import React from "react"
import containerStyles from "./test.module.css"
const TestContainer = ({ children }) => (
<section className={containerStyles.container}>{children}</section>
)
export default TestContainer;
最后我在 404 页面中使用了 TestContainer,虽然我认为只有生成的 404.html 页面会在头部包含 .container 样式,但 index.html 和 page-2.html 也包含样式.
我的项目包含大量 css,而 index.html 几乎是原来的两倍,因为它包含了整个应用程序中的所有 css。
我做错了什么还是有理由这样做?
【问题讨论】:
-
在构建项目时
TestContainer样式是否也可见,或者在运行gatsby develop命令时仅在development中可见?您可能还想查看gatsby-plugin-purgecss以删除所有未使用的CSS- gatsbyjs.org/packages/gatsby-plugin-purgecss -
我很惊讶地读到这篇文章并且实际上可以重现这种情况(在生产和开发版本中)。上面提到的插件只清理整个站点中未使用的 CSS,而不是每个页面。
-
你能出示你的404.html吗?