Replies: 5 comments 3 replies
-
There isn't a way to do that. The best solution would be making the environment variables from your server available at build time. If your server defined environment variables are set before you run your build command (ex: |
Beta Was this translation helpful? Give feedback.
-
Right now I'm doing it with automated search and replace on return Object.fromEntries(
Object.entries(this.props.environment || {})
.filter(([key]) => key.startsWith('NEXT_PUBLIC_'))
.map(([key, value]) => [`"{{ ${key} }}"`, `process.env.${key}`])
) But maybe there is a better way. My use case is I'm using AWS and doing a deployment with cloudformation. I don't know the values at build time of what all of my infrastructure resources will be when the deployment completes, like API URLs and such. But environment variables for the server handler will be resolved to the correct values. |
Beta Was this translation helpful? Give feedback.
-
I typically use Runtime Configuration in order to work with dynamic environment variables. I do not know if that will work for you, but what I do on my projects is use a package like envalid in the |
Beta Was this translation helpful? Give feedback.
-
This would be a really useful feature for me: disable inlining of public env vars for the sever code in a standalone build. |
Beta Was this translation helpful? Give feedback.
-
//next.config.js
import dotenv from "dotenv";
dotenv.config();
/** @type {import('next').NextConfig} */
const nextConfig = {
output: "standalone",
reactStrictMode: true,
experimental: {
cssChunking: "loose", // default
},
// env: process.env,
env: {
BALANCER_URL: process.env.BALANCER_URL,
NEXT_PUBLIC_WEBSOCKET_URI: process.env.NEXT_PUBLIC_WEBSOCKET_URI,
NEXT_PUBLIC_LLM_WEBSOCKET_URI: process.env.NEXT_PUBLIC_LLM_WEBSOCKET_URI,
},
};
export default nextConfig; i used - next-runtime-env import Logo from "@/components/Logo";
import "@/styles/globals.css";
import type { Metadata } from "next";
import { PublicEnvProvider } from "next-runtime-env";
import localFont from "next/font/local";
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en">
<body
suppressHydrationWarning={true}
>
<PublicEnvProvider>
{children}
</PublicEnvProvider>
</body>
</html>
);
} const { NEXT_PUBLIC_WEBSOCKET_URI, NEXT_PUBLIC_LLM_WEBSOCKET_URI } =
useEnvContext(); |
Beta Was this translation helpful? Give feedback.
-
I want to deploy a standalone server with configuration that I don't have at build time. I'm trying to create a CDK construct to deploy nextjs as a lambda function, during the build I don't know what values I'll have (e.g. API URL) until after the deployment finishes. I can provide these values as environment variables to the lambda function, but they don't get picked up if they begin with
NEXT_PUBLIC_
. Which I get for client-side code, but I want to share some of those configs on the server side.Is there perhaps some way to avoid the inlining of these vars in standalone mode?
Originally posted by @ijjk in #33910 (comment)
Beta Was this translation helpful? Give feedback.
All reactions