javascript - 如何在 Docusaurus V2 的 index.html 头部添加自定义脚本?

标签 javascript react-helmet docusaurus

我们正在使用 Docusaurus V2 制作一个网站。

在 Docusaurus V1 中,有一个 scripts设置在siteConfig.js自定义 html 的头部内容。但是,我在 Docusaurus V2 中找不到相应的设置。

根据https://docusaurus.io/blog/2018/09/11/Towards-Docusaurus-2#layout ,似乎可以自定义html的<head> V2 的一部分。

Layout

The current state of Docusaurus is that it is in charge of the entire layout and styling, unintentionally making it very hard for users to customize their site's appearance to their wishes.

For Docusaurus 2, layout and styling should be controlled by the user. Docusaurus will handle the content generation, routing, translation, and versioning. Inspired by create-react-app and VuePress, Docusaurus will still provide a default theme, which the user can eject from, for further layout and styling customization. This means that it is very possible for the user to even change the HTML meta by using React Helmet. Community-based themes are also very possible. This approach of allowing users to be in charge of layout and styling is taken by most static site generators.

我尝试使用react-helmetsrc/pages/index.js ,使用以下代码:

function Home() {
  const context = useDocusaurusContext();
  const { siteConfig = {} } = context;
  return (
    <Layout
      title={`Hello from ${siteConfig.title}`}
      description="Description will go into a meta tag in <head />">
      <Helmet>
        <script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"></script>
      </Helmet>
    </Layout>
  );
}

}

但是脚本https://appsforoffice.microsoft.com/lib/1/hosted/office.js没有出现在里面<head></head>

有人遇到过类似的问题吗?可以提供帮助吗?

最佳答案

更简单的方法:

创建文件/static/js/loadtags.js。将您的标签放在那里,例如:

UST_CT = [];UST = { s: Date.now(), addTag: function(tag) { UST_CT.push(tag) } };UST.addEvent = UST.addTag;

现在更新 docusaurus.config.js 中的脚本部分

  scripts: [
    
    {
      src:
        '/js/loadtags.js',
      async: false,
    },
    {
      src:
        'https://improve.attrace.com/server/ust.min.js?v=4.3.2',
      async: true,
    },
  ],

关于javascript - 如何在 Docusaurus V2 的 index.html 头部添加自定义脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57859350/

相关文章:

javascript - 在 Slider Bootstrap Carousel 中获取高度和宽度特定的 Div

javascript - 平滑滚动事件滚动

javascript - 如何以及在哪里访问 gatsby 中的 html 正文

documentation - 可以从多个存储库发布 docusaurus 交叉存储库,以将文档聚合在一起吗?

javascript - 'import' 和 'export' 可能仅与 'sourceType: "模块一起出现"' (16 :0)

javascript - PHP 上的返回页首链接是否包含页脚?

javascript - datatables-editable 插件在数据表中动态添加行后不起作用

javascript - 使用 php 后端和 redux 在 React js 中实现动态元标记

ms-office - 修复由 office.js 删除的 history.pushState 的最佳做法是什么?