You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Next.js站点Google AdSense头部代码未被识别的解决方法咨询

Next.js中Google AdSense无法识别代码的解决方法

针对你遇到的AdSense始终提示需将代码放在页面头部的问题,可通过以下步骤解决:

  • 替换为原生<script>标签
    Next.js的<Script>组件虽能实现脚本加载,但AdSense的验证爬虫更易识别原生标签。将原代码中的AdSense相关<Script>组件替换为原生<script>标签,确保直接嵌入在<head>内:

    修改后的_document.js代码:

    import { Html, Head, Main, NextScript } from 'next/document';
    import Script from 'next/script'
    
    function Document() {
      return (
        <Html>
          <Head>
            <link rel="icon" href="/images/logo1.ico" />
            <link rel="apple-touch-icon" href="/images/logo1.png" />
            <link rel="manifest" href="/images/manifest.json" />
            {/* 原生script标签适配AdSense检测 */}
            <script
              async
              src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"
              data-ad-client="ca-pub-**********"
            ></script>
            <Script
                src={"https://www.googletagmanager.com/gtag/js?id=G-*******"}
                strategy="afterInteractive"
              />
            <Script id="google-analytics" strategy="afterInteractive">
              {`
                window.dataLayer = window.dataLayer || [];
                function gtag(){dataLayer.push(arguments);}
                gtag('js', new Date());
    
                gtag('config', 'G-********');
              `}
            </Script>
        </Head>
          <body>
            <Main />
            <NextScript />
          </body>
        </Html>
      )
    }
    
    export default Document;
    
  • 验证脚本加载状态
    部署代码后,用Chrome开发者工具的「网络」面板确认adsbygoogle.js能正常加载,无404或加载失败情况。

  • 等待AdSense爬虫检测
    AdSense的页面验证并非实时,修改后需等待1-2小时,让爬虫重新抓取并识别页面中的代码。

  • 检查CSP策略(若有)
    若站点配置了内容安全策略(CSP),需确保允许加载https://pagead2.googlesyndication.com域名下的脚本,避免策略阻止AdSense代码运行。

内容的提问来源于stack exchange,提问作者maurijn.vd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 21:55:45