Google adsense - 在 gatsby 站点的 html.js 文件中出现问题

Google adsense - Issue in html.js file in gatsby site

我的 gatsby 网站获得了 google 的批准。我添加了 html.js 文件,并按照下面的线程中提到的那样做了同样的事情。

在 prod env(实时网站)中,我收到如下“意外令牌 &”错误:

https://i.imgur.com/nQ2x1o3.png

单击错误消息时,它指向 html.js 中的以下代码:

https://i.imgur.com/SN0yQYd.png

 <script>
          {`
                    (adsbygoogle = window.adsbygoogle || []).push({
                      google_ad_client: "ca-pub-xxxxxxx0300195",
                      enable_page_level_ads: true
                    });
                  `}
        </script>

我的html.js代码:

import React from "react"
import PropTypes from "prop-types"

export default function HTML(props) {
  return (
    <html {...props.htmlAttributes}>
      <head>
        <meta charSet="utf-8" />
        <meta httpEquiv="x-ua-compatible" content="ie=edge" />
        <meta
          name="viewport"
          content="width=device-width, initial-scale=1, shrink-to-fit=no"
        />
        <script
          async
          src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"
        ></script>
        <script>
          {`
                    (adsbygoogle = window.adsbygoogle || []).push({
                      google_ad_client: "ca-pub-xxxxxx30300195",
                      enable_page_level_ads: true
                    });
                  `}
        </script>
        {props.headComponents}
      </head>
      <body {...props.bodyAttributes}>
        {props.preBodyComponents}
        <div
          key={`body`}
          id="___gatsby"
          dangerouslySetInnerHTML={{ __html: props.body }}
        />
        {props.postBodyComponents}
      </body>
    </html>
  )
}

HTML.propTypes = {
  htmlAttributes: PropTypes.object,
  headComponents: PropTypes.array,
  bodyAttributes: PropTypes.object,
  preBodyComponents: PropTypes.array,
  body: PropTypes.string,
  postBodyComponents: PropTypes.array,
}

像这样使用它:

    <script
      type='text/javascript'
      dangerouslySetInnerHTML={{
        __html: `
         (adsbygoogle = window.adsbygoogle || []).push({
                  google_ad_client: "ca-pub-xxxxxxx0300195",
                  enable_page_level_ads: true
                })`,
      }}
    />

已申请:

import React from "react"
import PropTypes from "prop-types"

export default function HTML(props) {
  return (
    <html {...props.htmlAttributes}>
      <head>
        <meta charSet="utf-8" />
        <meta httpEquiv="x-ua-compatible" content="ie=edge" />
        <meta
          name="viewport"
          content="width=device-width, initial-scale=1, shrink-to-fit=no"
        />
        <script
          async
          src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"
        ></script>
        <script
          type='text/javascript'
          dangerouslySetInnerHTML={{
            __html: `
             (adsbygoogle = window.adsbygoogle || []).push({
                      google_ad_client: "ca-pub-xxxxxxx0300195",
                      enable_page_level_ads: true
                    })`,
          }}
        />
        {props.headComponents}
      </head>
      <body {...props.bodyAttributes}>
        {props.preBodyComponents}
        <div
          key={`body`}
          id="___gatsby"
          dangerouslySetInnerHTML={{ __html: props.body }}
        />
        {props.postBodyComponents}
      </body>
    </html>
  )
}

HTML.propTypes = {
  htmlAttributes: PropTypes.object,
  headComponents: PropTypes.array,
  bodyAttributes: PropTypes.object,
  preBodyComponents: PropTypes.array,
  body: PropTypes.string,
  postBodyComponents: PropTypes.array,
}