使用 NextJS 动态添加变量到外部 js 脚本
Dynamically add a variable to an external js script with NextJS
我的 nextjs
应用程序的 /static/js
中有这个外部 hotjar 脚本。
(function(h,o,t,j,a,r){
h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};
h._hjSettings={hjid:<SITEID>,hjsv:6};
a=o.getElementsByTagName('head')[0];
r=o.createElement('script');r.async=1;
r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv;
a.appendChild(r);
})(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv=');
我已将此文件从 _document.js
的 Head
部分导入到我的应用程序中,如下所示:
<script src={'/js/hotjar.js'} ></script>
问题:
我的 Nextjs 应用程序目前 运行 在暂存和实时环境中,我想为两者添加一个脚本。上面脚本的唯一动态部分是此处的 SITEID
值 h._hjSettings={hjid:<SITEID>,hjsv:6};
。我如何为配置文件中的不同环境添加不同的 SITEID
s 并动态更改此值,因为此脚本在客户端运行?
Edit:
you can use react-hotjar and simply
import { hotjar } from 'react-hotjar';
hotjar.initialize(hjid, hjsv);// Hotjar ID and Hotjar Snippet Version
Otherwise You have 2 options:
Option 1
first make sure your package.json
start script will set enviroment variable, something like this :
"scripts": {
...
"start": "cross-env NODE_ENV=production node server.js",
...
}
Then create 2 hotjar sripts, lets say /js/prod_hotjar.js
and /js/staging_hotjar.js
which have appropriate SITEID
inside.
Then in your _document.js
detect the current enviroment, and render the appropriate script with something like this :
import Document, { Html, Head, Main, NextScript } from 'next/document'
const prod= process.env.NODE_ENV === 'production'
class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx)
return { ...initialProps }
}
render() {
const url = prod ? "/js/prod_hotjar.js" : "/js/staging_hotjar.js"
return (
<Html>
<Head>
<script src={url} ></script>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument
Option 2
Use dangerouslySetInnerHTML
with something like this :
import Document, { Html, Head, Main, NextScript } from 'next/document'
const dev = process.env.NODE_ENV === 'production'
class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx)
return { ...initialProps }
}
render() {
const SITEID = prod ? 1234 : 4567 // or any other logic
return (
<Html>
<Head>
<script dangerouslySetInnerHTML={{__html: `(function(h,o,t,j,a,r){
h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};
h._hjSettings={hjid:${SITEID},hjsv:6};
a=o.getElementsByTagName('head')[0];
r=o.createElement('script');r.async=1;
r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv;
a.appendChild(r);
})(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv=');`}} />
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument
我的 nextjs
应用程序的 /static/js
中有这个外部 hotjar 脚本。
(function(h,o,t,j,a,r){
h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};
h._hjSettings={hjid:<SITEID>,hjsv:6};
a=o.getElementsByTagName('head')[0];
r=o.createElement('script');r.async=1;
r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv;
a.appendChild(r);
})(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv=');
我已将此文件从 _document.js
的 Head
部分导入到我的应用程序中,如下所示:
<script src={'/js/hotjar.js'} ></script>
问题:
我的 Nextjs 应用程序目前 运行 在暂存和实时环境中,我想为两者添加一个脚本。上面脚本的唯一动态部分是此处的 SITEID
值 h._hjSettings={hjid:<SITEID>,hjsv:6};
。我如何为配置文件中的不同环境添加不同的 SITEID
s 并动态更改此值,因为此脚本在客户端运行?
Edit:
you can use react-hotjar and simply
import { hotjar } from 'react-hotjar';
hotjar.initialize(hjid, hjsv);// Hotjar ID and Hotjar Snippet Version
Otherwise You have 2 options:
Option 1
first make sure your package.json
start script will set enviroment variable, something like this :
"scripts": {
...
"start": "cross-env NODE_ENV=production node server.js",
...
}
Then create 2 hotjar sripts, lets say /js/prod_hotjar.js
and /js/staging_hotjar.js
which have appropriate SITEID
inside.
Then in your _document.js
detect the current enviroment, and render the appropriate script with something like this :
import Document, { Html, Head, Main, NextScript } from 'next/document'
const prod= process.env.NODE_ENV === 'production'
class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx)
return { ...initialProps }
}
render() {
const url = prod ? "/js/prod_hotjar.js" : "/js/staging_hotjar.js"
return (
<Html>
<Head>
<script src={url} ></script>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument
Option 2
Use dangerouslySetInnerHTML
with something like this :
import Document, { Html, Head, Main, NextScript } from 'next/document'
const dev = process.env.NODE_ENV === 'production'
class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx)
return { ...initialProps }
}
render() {
const SITEID = prod ? 1234 : 4567 // or any other logic
return (
<Html>
<Head>
<script dangerouslySetInnerHTML={{__html: `(function(h,o,t,j,a,r){
h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};
h._hjSettings={hjid:${SITEID},hjsv:6};
a=o.getElementsByTagName('head')[0];
r=o.createElement('script');r.async=1;
r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv;
a.appendChild(r);
})(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv=');`}} />
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument