如何在Next.js的特定子域中隐藏脚本。

huangapple go评论66阅读模式
英文:

how i can hide Script in specific subDomain in nextjs

问题

在_document页面中我有一个用于聊天窗口的脚本我需要在任何子域名中隐藏该脚本

示例

`domain.com` >> 允许该脚本 ,,,

`*.domain.com` >> 禁止该脚本 

`*.domain.com/*` >> 禁止该脚本 

```jsx
import { Html, Head, Main, NextScript } from 'next/document'
const Document = () => {
  return (
    <Html lang="en">
      <Head>
        <script src="//code.jivosite.com/widget/id" async></script> // 这是脚本
      </Head>
      <body>
        <Main />
        <NextScript />
      </body>
    </Html>
  )
}
export default Document;
英文:

in the _document page, i have A Script for chat window and i need to hide the Script in any subDomain ,

example:

domain.com >> allow the Script ,,,,

*.domain.com >> disallow the Script

*.domain.com/* >> disallow the Script

import { Html, Head, Main, NextScript } from &#39;next/document&#39;
const Document = () =&gt; {
  return (
    &lt;Html lang=&quot;en&quot;&gt;
      &lt;Head&gt;
        &lt;script src=&quot;//code.jivosite.com/widget/id&quot; async&gt;&lt;/script&gt; // this is the script
      &lt;/Head&gt;
      &lt;body&gt;
        &lt;Main /&gt;
        &lt;NextScript /&gt;
      &lt;/body&gt;
    &lt;/Html&gt;
  )
}
export default Document;

答案1

得分: 1

我刚刚通过检查主机值来确定它是域名还是子域名,然后将结果传递给文档的props来解决了这个问题。

import { Html, Head, Main, NextScript } from 'next/document'
import NextDocument from "next/document"
import { domain } from '@/config/constants';

const Document = ({ isSubDomain }: any) => {

  return (
    <Html lang="en">
      <Head>
        {!isSubDomain && <script src="//code.jivosite.com/widget/id" async></script>}
      </Head>
      <body>
        <Main />
        <NextScript />
      </body>
    </Html>
  )
}

Document.getInitialProps = async (ctx: any) => {
  const initialProps = await NextDocument.getInitialProps(ctx);
  let isSubDomain = ctx?.req?.headers?.host?.replace(`.${domain}`, "") ? true : false
  return { ...initialProps, isSubDomain };
};

export default Document;
英文:

I have just resolved this issue by examining the host value to determine whether it is a domain or a subdomain, and then passing the result to the props of the Document.

import { Html, Head, Main, NextScript } from &#39;next/document&#39;
import NextDocument from &quot;next/document&quot;
import { domain } from &#39;@/config/constants&#39;;

const Document = ({ isSubDomain }: any) =&gt; {

  return (
    &lt;Html lang=&quot;en&quot;&gt;
      &lt;Head&gt;
        {!isSubDomain &amp;&amp; &lt;script src=&quot;//code.jivosite.com/widget/id&quot; async&gt;&lt;/script&gt;}
      &lt;/Head&gt;
      &lt;body&gt;
        &lt;Main /&gt;
        &lt;NextScript /&gt;
      &lt;/body&gt;
    &lt;/Html&gt;
  )
}

Document.getInitialProps = async (ctx: any) =&gt; {
  const initialProps = await NextDocument.getInitialProps(ctx);
  let isSubDomain = ctx?.req?.headers?.host?.replace(`.${domain}`, &quot;&quot;) ? true : false
  return { ...initialProps, isSubDomain };
};

export default Document;


huangapple
  • 本文由 发表于 2023年6月22日 20:33:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定