Title: Wrong environment configuration when in Vercel preview environment · Issue #6993 · getsentry/sentry-javascript · GitHub
Open Graph Title: Wrong environment configuration when in Vercel preview environment · Issue #6993 · getsentry/sentry-javascript
X Title: Wrong environment configuration when in Vercel preview environment · Issue #6993 · getsentry/sentry-javascript
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/...
Open Graph 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 th...
X 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 th...
Opengraph URL: https://github.com/getsentry/sentry-javascript/issues/6993
X: @github
Domain: github.com
IPv4 address: 140.82.113.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Wrong environment configuration when in Vercel preview environment","articleBody":"### Is there an existing issue for this?\r\n\r\n- [X] I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues\r\n- [X] I have reviewed the documentation https://docs.sentry.io/\r\n- [X] I am using the latest SDK release https://github.com/getsentry/sentry-javascript/releases\r\n\r\n### How do you use Sentry?\r\n\r\nSentry Saas (sentry.io)\r\n\r\n### Which package are you using?\r\n\r\n@sentry/nextjs\r\n\r\n### SDK Version\r\n\r\n7.34.0\r\n\r\n### Framework Version\r\n\r\nNext.js 13.0.4\r\n\r\n### Link to Sentry event\r\n\r\n_No response_\r\n\r\n### SDK Setup\r\n\r\n_No response_\r\n\r\n### Steps to Reproduce\r\n\r\n1. Create a new Next.js app with Sentry\r\n2. Deploy on Vercel\r\n3. Enable Sentry integration\r\n4. Deploy the app in a Vercel preview environment\r\n5. Open the preview deployment URL\r\n6. Simulate an error\r\n\r\nIf 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.\r\n\r\nThis 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.\r\n\r\n### Expected Result\r\n\r\nThe environment value should be set to the actual environment value coming from Vercel.\r\n\r\nThis can be solved in both ways:\r\n\r\n1. Have the Vercel integration add another environment variable (`NEXT_PUBLIC_SENTRY_ENVIRONMENT`) that is equal to the value of `NEXT_PUBLIC_VERCEL_ENV`, and prioritize it over `NODE_ENV`.\r\n2. Make Sentry code support the `NEXT_PUBLIC_VERCEL_ENV` value by default\r\n\r\nUntil then, one can use the following workaround in all the Sentry configuration files:\r\n\r\n```typescript\r\nSentry.init({\r\n dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,\r\n // We need to explicitly set the environment here, otherwise it will be set to \"production\" even\r\n // when the app running in a preview environment. This is because the environment configuration\r\n // defaults to NODE_ENV which is \"production\" when built with `pnpm build` and \"development\" when\r\n // running `pnpm dev`.\r\n environment: process.env.NEXT_PUBLIC_VERCEL_ENV,\r\n // ...\r\n});\r\n```\r\n\r\n### Actual Result\r\n\r\n\u003cimg width=\"336\" alt=\"CleanShot 2023-01-31 at 16 15 24@2x\" src=\"https://user-images.githubusercontent.com/4291707/215784433-238dff8c-9575-48b1-9926-0ae0d2d9b61c.png\">\r\n","author":{"url":"https://github.com/IGassmann","@type":"Person","name":"IGassmann"},"datePublished":"2023-01-31T14:29:19Z","url":"https://github.com/getsentry/sentry-javascript/issues/6993"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d6fc5428-4a09-9c66-05e3-4a40d017a698 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AF44:24CE74:5A0E:74A0:6B04C505 |
| html-safe-nonce | 5c7edc045546a1fedaf0602b2eb845a577c8fd7fccc359830b243525fd97bc86 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRjQ0OjI0Q0U3NDo1QTBFOjc0QTA6NkIwNEM1MDUiLCJ2aXNpdG9yX2lkIjoiMjI2NzIyODc1MTA1MjE5NTA3NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 79d1d19c1a15e4c05befed057e78f3fa9d079d0ba58ad37d37bc2e4da792e9a3 |
| hovercard-subject-tag | issue:1564422147 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/getsentry/sentry-javascript/6993/issue_layout |
| twitter:image | https://opengraph.githubassets.com/70ec621b7c21dbb30ffd0be1a15b5bdb5105b812de9e4a400b282a41d9e261d1/getsentry/sentry-javascript/issues/6993 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/70ec621b7c21dbb30ffd0be1a15b5bdb5105b812de9e4a400b282a41d9e261d1/getsentry/sentry-javascript/issues/6993 |
| og:image:alt | 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 th... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | IGassmann |
| hostname | github.com |
| expected-hostname | github.com |
| None | 85dd75377eb77313206b8fc108bcee6a3a029286d94bf32bf9b4426e4d92e0c4 |
| turbo-cache-control | no-preview |
| go-import | github.com/getsentry/sentry-javascript git https://github.com/getsentry/sentry-javascript.git |
| octolytics-dimension-user_id | 1396951 |
| octolytics-dimension-user_login | getsentry |
| octolytics-dimension-repository_id | 3328572 |
| octolytics-dimension-repository_nwo | getsentry/sentry-javascript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 3328572 |
| octolytics-dimension-repository_network_root_nwo | getsentry/sentry-javascript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 0d64fa748a2bab9a5b587a967ac4da1cfa8da6dd |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width