Description
Is there an existing issue for this?
- I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues
- I have reviewed the documentation https://docs.sentry.io/
- I am using the latest SDK release https://github.com/getsentry/sentry-javascript/releases
How do you use Sentry?
Sentry Saas (sentry.io)
Which package are you using?
@sentry/nextjs
SDK Version
7.34.0
Framework Version
Next.js 13.0.4
Link to Sentry event
No response
SDK Setup
No response
Steps to Reproduce
- Create a new Next.js app with Sentry
- Deploy on Vercel
- Enable Sentry integration
- Deploy the app in a Vercel preview environment
- Open the preview deployment URL
- Simulate an error
If you inspect the event produced by the simulated error, you will see that the environment
value is set to production
, instead of preview
. This can lead developers to try to solve issues that were actually only happening in the preview environments. It also pollutes production performance metrics with values that are coming from the preview environments.
This is happening because, by default, Sentry gets its environment value from NODE_ENV
. However, this value will always be set to production
when the project is built with next build
and to development
when the project is built with next dev
. This is due to how Webpack works. To work around this issue, Vercel provides the VERCEL_ENV
(available on the server only) and NEXT_PUBLIC_VERCEL_ENV
(available both on the browser and server) environment variables, which contain the correct environment value.
Expected Result
The environment value should be set to the actual environment value coming from Vercel.
This can be solved in both ways:
- Have the Vercel integration add another environment variable (
NEXT_PUBLIC_SENTRY_ENVIRONMENT
) that is equal to the value ofNEXT_PUBLIC_VERCEL_ENV
, and prioritize it overNODE_ENV
. - Make Sentry code support the
NEXT_PUBLIC_VERCEL_ENV
value by default
Until then, one can use the following workaround in all the Sentry configuration files:
Sentry.init({
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
// We need to explicitly set the environment here, otherwise it will be set to "production" even
// when the app running in a preview environment. This is because the environment configuration
// defaults to NODE_ENV which is "production" when built with `pnpm build` and "development" when
// running `pnpm dev`.
environment: process.env.NEXT_PUBLIC_VERCEL_ENV,
// ...
});