You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We'd like the ability to use environment variables at runtime in middleware.
It is a common application practice to build & bundle an app once and then configure it based on runtime environment variables. This allows us to adhere to being a 12 factor app as much as possible. We can do this with the rest of nextjs, but not middleware.
Describe the solution you'd like
I'm unsure what the mechanism should be because you currently replace process.env.FOO at build time, but there should be some ability to read it. Perhaps make serverRuntimeConfig specially available in middleware? Perhaps make certain env vars available if they're prefixed with MIDDLEWARE_ like the established pattern for prefixing PUBLIC_ vars ?
Describe alternatives you've considered
We previously were able to use optional chaining to get around this and process.env?.FOO worked at runtime in middleware, but as there were changes at some point between nextjs versions this ability has disappeared and there appears to be no way to access any environment variables at run time.
The only other solution we can think of is mentioned here to perhaps load a JSON file at deployment time with config that would be included in the app, but it is somewhat awkward to use and requires changes to the way env vars are set by deployment systems and generally doesn't feel right. It'd be better if environment variables functioned as they do in other parts of the application.
The text was updated successfully, but these errors were encountered:
Describe the feature you'd like to request
We'd like the ability to use environment variables at runtime in middleware.
It is a common application practice to build & bundle an app once and then configure it based on runtime environment variables. This allows us to adhere to being a 12 factor app as much as possible. We can do this with the rest of nextjs, but not middleware.
Describe the solution you'd like
I'm unsure what the mechanism should be because you currently replace
process.env.FOO
at build time, but there should be some ability to read it. Perhaps makeserverRuntimeConfig
specially available in middleware? Perhaps make certain env vars available if they're prefixed withMIDDLEWARE_
like the established pattern for prefixingPUBLIC_
vars ?Describe alternatives you've considered
We previously were able to use optional chaining to get around this and
process.env?.FOO
worked at runtime in middleware, but as there were changes at some point between nextjs versions this ability has disappeared and there appears to be no way to access any environment variables at run time.The only other solution we can think of is mentioned here to perhaps load a JSON file at deployment time with config that would be included in the app, but it is somewhat awkward to use and requires changes to the way env vars are set by deployment systems and generally doesn't feel right. It'd be better if environment variables functioned as they do in other parts of the application.
The text was updated successfully, but these errors were encountered: