简体   繁体   English

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

[英]How can I add custom scripts in index.html's head part in Docusaurus V2?

We are making a website with Docusaurus V2.我们正在使用 Docusaurus V2 制作网站。

In Docusaurus V1, there is a scripts setting in siteConfig.js to cusutimize html's head content.在 Docusaurus V1 中,siteConfig.js 中有一个scripts设置, siteConfig.js自定义 html 的头部内容。 But, I cannot find the corresponding setting in Docusaurus V2.但是,我在 Docusaurus V2 中找不到相应的设置。

According to https://docusaurus.io/blog/2018/09/11/Towards-Docusaurus-2#layout , it seems possible to customize html's <head> part in V2.根据https://docusaurus.io/blog/2018/09/11/Towards-Docusaurus-2#layout ,似乎可以在 V2 中自定义 html 的<head>部分。

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. Docusaurus 的当前状态是它负责整个布局和样式,无意中使用户很难根据自己的意愿自定义站点的外观。

For Docusaurus 2, layout and styling should be controlled by the user.对于 Docusaurus 2,布局和样式应由用户控制。 Docusaurus will handle the content generation, routing, translation, and versioning. Docusaurus 将处理内容生成、路由、翻译和版本控制。 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.受 create-react-app 和 VuePress 的启发,Docusaurus 仍将提供一个默认主题,用户可以从中弹出,以进行进一步的布局和样式定制。 This means that it is very possible for the user to even change the HTML meta by using React Helmet.这意味着用户甚至可以使用 React Helmet 更改 HTML 元数据。 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.大多数静态站点生成器都采用这种允许用户负责布局和样式的方法。

I tried to use react-helmet in src/pages/index.js , with the following code:我尝试在src/pages/index.js使用react-helmet ,代码如下:

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>
  );
}

} }

But the script https://appsforoffice.microsoft.com/lib/1/hosted/office.js did not show up inside <head></head>但是脚本https://appsforoffice.microsoft.com/lib/1/hosted/office.js没有出现在<head></head>

Has anyone encountered similar problem and could anyone give some help?有没有人遇到过类似的问题,谁能提供一些帮助?

Instead of React Helmet, use '@docusaurus/Head' instead.使用'@docusaurus/Head'代替 React Helmet。

import Head from '@docusaurus/Head';

function Home() {
  const context = useDocusaurusContext();
  const { siteConfig = {} } = context;
  return (
    <Layout>
      <Head>
        <script src="..."></script>
      </Head>
    </Layout>
  );
}

We're working on this feature so you can add this via docusaurus.config.js .我们正在开发此功能,因此您可以通过docusaurus.config.js添加此docusaurus.config.js You can follow this PR to track the progress: https://github.com/facebook/docusaurus/pull/1831 .您可以按照此 PR 跟踪进度: https : //github.com/facebook/docusaurus/pull/1831

We'll release v2.0.0-alpha.27 soon so that you can try it out.我们将很快发布v2.0.0-alpha.27以便您可以试用。 Thanks for your patience!谢谢你的耐心!

i am also developing a blog which is based on docusaurus.我也在开发一个基于 docusaurus 的博客。
and it provides the functionality to add script in head tag.它提供了在 head 标签中添加脚本的功能。
follow below steps :请按照以下步骤操作:
1. Open siteConfig.js 1.打开siteConfig.js
2. // Add custom scripts here that would be placed in tags. 2. // 在此处添加将放置在标签中的自定义脚本。
scripts: [' https://buttons.github.io/buttons.js '],脚本:[' https://buttons.github.io/buttons.js '],

I have no idea if you solved this.我不知道你是否解决了这个问题。 Your friend is the Lifecycle API.您的朋友是 Lifecycle API。

Specifically you want this: https://v2.docusaurus.io/docs/lifecycle-apis#injecthtmltags具体你想要这个: https : //v2.docusaurus.io/docs/lifecycle-apis#injecthtmltags

Build a plugin and use the above.构建一个插件并使用上面的。 D2 is looking pretty strong, I wish they'd release a stable version 2 soon! D2 看起来很强大,我希望他们尽快发布稳定版 2!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM