NextJS根据ENV激活DataDogrum

发布于 2025-01-31 16:45:49 字数 311 浏览 3 评论 0原文

我们目前有多种环境,我们只想启动DataDogrum以进行生产。

在_app.tsx中,我尝试了这样的事情:

import { datadogRum } from '@datadog/browser-rum';

if (process.env.NODE_ENV=== 'production') {
  datadogRum.init({...});
  datadogRum.startSessionReplayRecording();
}

但是这是不起作用的,因为这里没有env变量...

您对此有任何工作吗?

We currently have multiple environments and we would like to init datadogRum only for production.

In the _app.tsx I tried something like this :

import { datadogRum } from '@datadog/browser-rum';

if (process.env.NODE_ENV=== 'production') {
  datadogRum.init({...});
  datadogRum.startSessionReplayRecording();
}

But this is not working because env variables are not available here ...

Do you have any work around for this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

话少情深 2025-02-07 16:45:49

我成功使用@BWest评论。

我使用nextjs getInitialProps是为了传输我的env变量。

然后,我将我的datadog init代码放在useffect(()=> {...},[]) hook中仅执行代码一次。

I succeed using @bwest comment.

I used nextJs getInitialProps in order to transmit my env variable.

And I put my datadog init code in a useEffect(() => {...} , []) hook to only execute the code once.

偷得浮生 2025-02-07 16:45:49

使用NextJS环境变量可用于前端代码。

它们将使用某些WebPack插件(例如EnvironmentPlugin或DefinePlugin)在您的代码中进行插值。

从此

next.js具有对环境变量的内置支持,这允许您执行以下操作:

  • 使用.env.local加载环境变量
  • 将环境变量曝光到浏览器中,通过next_public _
  • 前缀

With nextjs environment variables can be available to front-end code.

They will be interpolated inside your code using some webpack plugin (something like EnvironmentPlugin or DefinePlugin).

From this Nextjs documentation:

Next.js comes with built-in support for environment variables, which allows you to do the following:

  • Use .env.local to load environment variables
  • Expose environment variables to the browser by prefixing with NEXT_PUBLIC_
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文