Custom preview domain sets NEXT_PUBLIC_VERCEL_ENV to 'production' #775
Replies: 2 comments 10 replies
-
Thanks for sharing this writeup @karmaniverous, and I'm sorry you're running into this problem. I was not able to reproduce the issue in my own project with two different custom domains (one using CNAME Record and the other using Vercel nameservers). I have a couple of initial ideas, though you may have already thought of them:
If anyone else has run into this problem with preview environment variables, I hope they will share their experience here as well! This is not something I have seen before. Curious to understand what is happening to cause this behavior. |
Beta Was this translation helpful? Give feedback.
-
We had this problem too. We're not using Vercel CLI or the GitHub integration to deploy to our preview branch, which we have named "staging." We want to use process.env.VERCEL_ENV while rendering a custom _document.js in our next.js app in order to add a noindex,nofollow meta tag to the tag; however, process.env.VERCEL_ENV was always 'production' in both preview and production. (We added a comment to our inline google-tag-manager script to show the value, seen below in the html snippet.) Now here's the weird part: Our staging subdomain was "signup.staging" when we observed process.env.VERCEL_ENV always being "production." Then on a hunch, we changed the subdomain to "signup-staging" so there were only 3 segments to the hostname (for example, "signup-staging.example.com" For some reason, this change fixed the issue. Now process.env.VERCEL_ENV === 'preview' at signup-staging.example.com and "production" at signup.example.com export default class MyDocument extends Document {
render() {
return (
<Html lang='en'>
<Head>
{process.env.VERCEL_ENV !== 'production' && (
<meta name='robots' content='noindex,nofollow' />
)}
<Script id='google-tag-manager' strategy='beforeInteractive'>
{`
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXX');
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-99999999-1', {'transport_type': 'image'});
// VERCEL_ENV = "${process.env.VERCEL_ENV}"
`}
</Script> |
Beta Was this translation helpful? Give feedback.
-
I opened a ticket on this one but there doesn't seem to be a way to link back to it, so I thought I would also add a discussion here. Here's a full writeup of the issue along with my workaround.
Beta Was this translation helpful? Give feedback.
All reactions