Closed freaz closed 6 months ago
Hi,
I think this is expected behaviour on Edge:
Good to know: When deploying your Next.js application to Vercel, your environment variables in .env* files will not be made available to Edge Runtime, unless their name are prefixed with NEXTPUBLIC. We strongly recommend managing your environment variables in Project Settings instead, from where all environment variables are available.
https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables
https://github.com/vercel/next.js/discussions/39705 has some more information too.
I worked around the problem by running a script as part of our CI process that takes environment variables from the .env
files and uses the Vercel API to add them as environment variables to the Vercel project settings, being careful not to override any variables that we don't want to be accidentally overridden.
Hey @philwolstenholme, thanks for the reply. You are right. Unfortunately the main use case for .env.production
is to set ENV variables based on values set by Vercel. For example VERCEL_BRANCH_URL
to setup Auth0 (setting AUTH0_BASE_URL for previews).
With the current state, it requires either manual deployment step setting AUTH0_BASE_URL
or using non-default variable names in the code.
Also, it is quite confusing (and bad DX) to have ENV variables configured in the dashboard available in runtime and from .env*
files only with a public prefix.
I understand now how it works for end users, and can imagine what is going on when code is deployed to the edge. So this isn't a bug per se, but I still believe there is space for improvement.
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.
Link to the code that reproduces this issue
https://github.com/freaz/nextjs_edge_env_files
To Reproduce
use
.env
,.env.production
, ... to set default values (.e.g:AUTH0_BASE_URL="https://$VERCEL_BRANCH_URL"
), than use the value with page or route using edge runtime.Current vs. Expected behavior
On
nodejs
runtime env variable is properly set, but onedge
it is missing. Expected is to load env vars from.env
,.env.production
, ... files the same way as fornodejs
runtime.Provide environment information
Which area(s) are affected? (Select all that apply)
Runtime
Which stage(s) are affected? (Select all that apply)
Vercel (Deployed)
Additional context
No response