Consider a Next.js app that uses different API endpoints for development and production. Why must deployment configuration handle environment variables carefully?
Think about how the app talks to different servers in development vs production.
Environment variables let the app know which backend URLs or keys to use. This prevents errors like calling a test server in production.
In Next.js, environment variables prefixed with NEXT_PUBLIC_ are exposed to the browser. What is the effect if you omit this prefix for a variable used in client-side code?
console.log(process.env.API_URL);
Remember which variables Next.js exposes to client-side code.
Only variables prefixed with NEXT_PUBLIC_ are included in the client bundle. Others are only available server-side.
Which code snippet correctly accesses a public environment variable NEXT_PUBLIC_API_URL in a Next.js component?
Recall how Next.js exposes environment variables in code.
Next.js uses process.env to access environment variables. Only those prefixed with NEXT_PUBLIC_ are available in client code.
Given this Next.js component:
export default function Home() {
return <div>API URL: {process.env.NEXT_PUBLIC_API_URL}</div>;
}If NEXT_PUBLIC_API_URL is not set in deployment, what will the rendered output be?
export default function Home() { return <div>API URL: {process.env.NEXT_PUBLIC_API_URL}</div>; }
What does JavaScript show when a variable is not defined?
If the environment variable is missing, process.env.NEXT_PUBLIC_API_URL is undefined, so it renders as the string 'undefined'.
You updated environment variables on your hosting platform, but your Next.js app still shows old values after redeploy. What is the most likely cause?
Think about when Next.js reads environment variables.
Next.js reads environment variables during build. Changing them requires rebuilding and redeploying the app to update values.