下一个脚本在 _document.js 文件 nextjs 中不起作用

Tap*_*Das 6 next.js

我有一个 nextjs 项目。我想在 _document.js 中使用 next/script 加载两个脚本。但是当我将 Script 标签放入 _document.js 中的 body 标签时,我的脚本不会执行。我按照下一个/脚本指南实施。这里可能有什么问题?

我的代码:

import { Html, Head, Main, NextScript } from "next/document";
import Script from "next/script";
export default function Document() {
 return (
   <Html>
     <Head>
       <link rel="preconnect" href="https://fonts.googleapis.com" />        
     </Head>
     <body>
       <Main />
       <NextScript />
         <Script
         strategy="beforeInteractive"
         src="src"
         type="text/javascript"
         charSet="UTF-8"
         data-domain-script="id"
       />
       <Script
         strategy="beforeInteractive"
         type="text/javascript"
         dangerouslySetInnerHTML={{
           __html: `
           some js code
     `,
         }}
       />
     </body>
   </Html>
 );
}

Run Code Online (Sandbox Code Playgroud)

Tap*_*Das 3

最初在 next/script api 参考部分中,记录了在 _document.js 中使用beforeInteractive策略的 Script 标签。但现在他们的api 参考已经更新,你必须在pages/_app.js.

API参考链接

引用自 next/script api 参考

beforeInteractive 脚本必须放置在pages/_app.js 内,并且旨在加载整个站点所需的脚本(即,当应用程序中的任何页面加载到服务器端时,该脚本将加载)。