Complete the code to access an environment variable in a Next.js component.
const apiUrl = process.env.[1];In Next.js, environment variables that need to be exposed to the browser must start with NEXT_PUBLIC_.
Complete the code to load environment variables from a .env file in Next.js.
export default function handler(req, res) {
const secret = process.env.[1];
res.status(200).json({ secret });
}Server-side environment variables can be accessed directly without the NEXT_PUBLIC_ prefix.
Fix the error in the code to correctly use environment variables in Next.js API route.
export default function handler(req, res) {
const dbPassword = process.env.[1];
res.status(200).json({ dbPassword });
}Server-side code should use the exact environment variable name without the NEXT_PUBLIC_ prefix.
Fill both blanks to create a client-side component that safely uses a public environment variable.
export default function ApiUrl() {
const apiUrl = process.env.[1];
return <p>API URL is: [2]</p>;
}Use NEXT_PUBLIC_API_URL to access the variable and {apiUrl} inside JSX with braces to display the value.
Fill all three blanks to create a server-side function that reads and returns environment variables.
export async function getServerSideProps() {
const secret = process.env.[1];
const publicKey = process.env.[2];
return {
props: { secret, [3] }
};
}Server-side code accesses SECRET_KEY directly, public variables start with NEXT_PUBLIC_, and the returned props must match variable names.