gatsby - Gatsby JS-如何在 gatsbyjs 的头部内正确添加 html 注释?
问题描述
我按照 gatsbys 网站上的说明进行了以下操作:
- cp .cache/default-html.js src/html.js
- 进入(由 gatsby 创建的)html.js 文件并在 head 部分添加注释。
- 而不是我使用 {/* */} 作为第一个抛出错误
- 当我运行 gatsby develop 时,我首先收到一个错误,加载/查询 404 页面,然后是其他页面
有谁知道这是怎么做到的吗?谢谢你。
解决方案
根据文档:
将 HTML 插入到
<head>
您在组件中呈现的任何内容
html.js
都不会像其他组件一样在客户端中“活动”。如果你想动态更新你的我们推荐使用React Helmet
因此,您不能直接使用文件自定义<head>
标签。html.js
此外,Gatsby 摆脱了 SSI 评论(您可以在此 GitHub 线程中跟踪堆栈跟踪)。
这么说的。您可以尝试使用gatsby-plugin-html-comments
:
module.exports = {
{
resolve: `gatsby-plugin-html-comments`,
options: {
files: ['./public/**/*.html', './public/*.html'],
comment: [
{
regexp: /<keep-this-comment-tag>(.*?)<\/keep-this-comment-tag>/g,
comment: `<!-- keep this comment -->`,
},
]
}
}
}
将输出:
<div>
<!-- keep this comment -->
<h1>Hello World</h1>
</div>
您还可以使用 Gatsby 公开的 API 之一来自定义输出,onPostBuild
应该适合您。复制html/js
然后添加自定义组件(例如<ssi-code />
然后使用replace-in-file替换它们:
replace.sync({
files: ['./public/**/*.html', './public/*.html'],
from: /<SSI-before-html>(.*?)<\/SSI-before-html>/g,
to: '<!--#include virtual="/virtual/example.inc"-->',
});
资料来源:如何在 Gatsby 应用程序中插入 SSI 评论?
此外,您可以按照线程的建议:
<div dangerouslySetInnerHTML={{ __html: '<!--#set var="section" value="#{section}"-->'}}></div>
推荐阅读
- angular - 'HttpRequest 类型的参数
' 不能分配给 'string | 类型的参数 要求' - python - 如何保持字典的顺序并上传到服务器?
- safari - Safari HTML5 播放器中的 X-Playback-Session-Id 标头是什么?
- php - 在 wordpress 自定义主题索引上显示页面链接
- php - WordPress博客文章布局彼此不同
- rotation - paperjs旋转的问题
- mule - Mule 4 是否建议不要使用 Groovy 之类的脚本语言?
- wordpress - 如何为页面添加独特的菜单?
- python - 使用 django ORM 查询对 jsondata-> 字段进行计数聚合
- python - Apache Beam python fails to parse pubmed XML