reactjs - 如何在 Gatsby 应用程序中插入 SSI 注释?
问题描述
我正在构建一个 Gatsby 应用程序,我需要插入一些 SSI 行(样式为 HTML 注释)。例子
<!--#set var="section" value="#{section}"-->
<!--#include virtual="/virtual/3.0/script-app.inc"-->
我找不到如何做到这一点。
我需要将这些(和其他脚本)放在标签之前、head
标签内、head
标签末尾body
和紧随其后。
我尝试了很多方法,但没有一个有效。我尝试过更改 html.js,但它只是从输出的代码中删除了注释。我也尝试过使用 gatsby-ssr 和 gatsby-browser,但我一直在想我不知道在这些文档中到底要做什么。
我期望输出是什么(带有示例代码):
<!DOCTYPE html>
<!--#set var="section" value="#{section}"-->
<html>
<head>
<!--#include virtual="/virtual/3.0/script-app.inc"-->
[rest of head code]
</head>
<body>
</html>
会发生什么:我的输出页面没有任何评论或已将其字符串化(如<!--#include virtual="/virtual/3.0/script-app.inc"-->
解决方案
我使用gatsby-node.js
and解决了这个问题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"-->',
});
推荐阅读
- batch-file - Windows 10 批处理文件:在以前版本中工作的文件的奇怪行为
- gcc - 汇编代码生成 SEGMENTATION FAULT
- xbee - XBee3 协调器在网络发现期间找不到 End_Device
- java - Java 自动装箱 int 到 Long
- postgresql - 无法使用通过 ansible 创建的 postgresql 用户登录
- ios - didWriteValueFor 在尝试使用 .writeValue 写入时未第二次调用
- internet-explorer - 启用代理服务器时互联网速度慢
- python-3.x - AttributeError:“NoneType”对象没有属性“大小”
- css - 如何删除css flex包含之间的空间
- javascript - 如何使用 Chai 进行测试