英文:
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 'next/document'
const Document = () => {
return (
<Html lang="en">
<Head>
<script src="//code.jivosite.com/widget/id" async></script> // this is the script
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
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 '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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论