-
Describe the feature you'd like to requestWe'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 likeI'm unsure what the mechanism should be because you currently replace Describe alternatives you've consideredWe previously were able to use optional chaining to get around this and 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. |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 14 replies
-
Facing similar issue for e2e testing, I need to setup NEXT_PUBLIC_NODE_ENV and NODE_ENV to test at runtime. import { NextFetchEvent, NextRequest, NextResponse } from "next/server";
export function middleware(req: NextRequest, ev: NextFetchEvent) {
// @see https://nextjs.org/docs/messages/middleware-relative-urls
// FIXME: 05/2022 this won't work as expected, env variables are set at build-time: https://github.com/vercel/next.js/discussions/36338
if (
process.env.NODE_ENV === "production" &&
process.env.NEXT_PUBLIC_NODE_ENV !== "test"
) {
const url = req.nextUrl.clone();
url.pathname = "/";
return NextResponse.redirect(url);
}
// In dev and test mode we can access the pages
return NextResponse.next();
} This protects my debug routes. As a palliative I've defined a build-time value "NEXT_PUBLIC_IS_LOCAL" in |
Beta Was this translation helpful? Give feedback.
-
There's the use-case of distributing a prebuilt Next.js application as a Docker image. Allowing users to configure their instance through environment variables when they run a container from your image. How can the Next.js application access these variables in middleware? edit I just noticed that |
Beta Was this translation helpful? Give feedback.
-
it must be how does @beam-australia/react-env work. I've been using it in normal(?) nextjs code so far, but even this method seems not working in middleware. As @Janpot says, our company also use docker image when deploying. Build process and run process are separated. So we also need a runtime env feature to apply different env var to each runtime container. |
Beta Was this translation helpful? Give feedback.
-
Looks like runtime environment variables work in nextjs version 12.2.0. Not sure if it was introduced in a 12.1.* version as @Janpot mentions it working last month. |
Beta Was this translation helpful? Give feedback.
-
Hi! 👋 We've created a package that adds support for runtime variables in Next.js: https://github.com/expatfile/next-runtime-env. Feel free to give it a try! 😉 |
Beta Was this translation helpful? Give feedback.
-
I'm a bit confused here as I'm seeing conflicting reports:
Could someone share more specifics, including their code snippet + Next.js version? |
Beta Was this translation helpful? Give feedback.
-
Hey everyone – we have just published new documentation on self-hosting, including how to configure a custom cache handler for use with ISR / Data Cache in both the Pages and App Router. These docs go in depth about how other features like Image Optimization, Middleware, Environment Variables, Build Caching, and more work when self-hosted. There's also updated Docker based examples. Specific to this discussion, we have provided guidance on how to use runtime environment variables. https://nextjs.org/docs/app/building-your-application/deploying |
Beta Was this translation helpful? Give feedback.
Hey everyone – we have just published new documentation on self-hosting, including how to configure a custom cache handler for use with ISR / Data Cache in both the Pages and App Router. These docs go in depth about how other features like Image Optimization, Middleware, Environment Variables, Build Caching, and more work when self-hosted. There's also updated Docker based examples.
Specific to this discussion, we have provided guidance on how to use runtime environment variables.
https://nextjs.org/docs/app/building-your-application/deploying