Heim > Fragen und Antworten > Hauptteil
P粉8181258052023-08-26 00:03:41
我重新阅读了之前的帖子Next 11 and adding Script tags not working. No scripts are rendered enter link description here,意识到你不能把<Script>
组件放在Head标签中。此外,它不应该在_document.tsx中,而应该在_app.tsx中(除非你使用beforeInteractive,参见上面的链接)。
因为我还想包含Google Analytics脚本,所以我创建了一个名为TrackingCode的组件作为单独的js文件。
import Script from "next/script"; function TrackingCode() { return ( <> {/* Global site tag (gtag.js) - Google Analytics */} <Script src="https://www.googletagmanager.com/gtag/js?id=G-GOOGLEID" /> <Script id="google-analytics" strategy="afterInteractive"> {` window.dataLayer = window.dataLayer || []; function gtag(){window.dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-GOOGLEID'); `} </Script> {/* for Zoho Marketing Automation */} <Script id="zoho-ma"> {`var w = window; var p = w.location.protocol; if (p.indexOf("http") < 0) { p = "http" + ":"; } var d = document; var f = d.getElementsByTagName("script")[0], s = d.createElement("script"); s.type = "text/javascript"; s.async = false; if (s.readyState) { s.onreadystatechange = function () { if (s.readyState == "loaded" || s.readyState == "complete") { s.onreadystatechange = null; try { loadwaprops( "mycode", "mycode", "mycode", "mycode", "0.0" ); } catch (e) {} } }; } else { s.onload = function () { try { loadwaprops( "mycode", "mycode", "mycode", "mycode", "0.0" ); } catch (e) {} }; } s.src = p + "//ma.zoho.com/hub/js/WebsiteAutomation.js"; f.parentNode.insertBefore(s, f);`} </Script> {/* end Zoho marketing automation */} </> ); } export default TrackingCode;
我的_app.tsx文件如下:
import "../assets/scss/material-kit.scss"; import "../node_modules/bootstrap/scss/bootstrap.scss"; import "../styles/globals.scss"; import { useEffect } from "react"; import type { ReactElement, ReactNode } from "react"; import type { NextPage } from "next"; import type { AppProps } from "next/app"; import TrackingCode from "../components/tracking-code"; import store from "../app/store"; export type NextPageWithLayout = NextPage & { getLayout?: (page: ReactElement) => ReactNode; }; type AppPropsWithLayout = AppProps & { Component: NextPageWithLayout; }; export default function MyApp({ Component, pageProps }: AppPropsWithLayout) { const getLayout = Component.getLayout ?? ((page) => page); useEffect(() => { typeof document !== undefined ? require("../node_modules/bootstrap/dist/js/bootstrap") : null; }, []); return getLayout( <> <TrackingCode /> <Component {...pageProps} /> </> ); }