Consider a Remix app that uses environment variables. Which statement correctly describes how environment variables are accessed in server and client code?
Think about security: client code should not expose secret variables.
Remix exposes environment variables to server code via process.env. To expose variables to client code, they must be prefixed with PUBLIC_. This prevents leaking secrets to the browser.
Given a Remix loader function, which snippet correctly reads an environment variable named API_KEY for server-side use?
export async function loader() {
// read API_KEY here
}Loader runs on the server, so use the standard Node.js environment variable access.
Loader functions run on the server, so process.env.API_KEY accesses the variable. import.meta.env is not used in Remix. window is undefined on server. PUBLIC_API_KEY is for client exposure only.
Given this client component code:
export default function MyComponent() {
const apiUrl = process.env.API_URL;
return <div>API URL: {apiUrl}</div>;
}Why does apiUrl show as undefined in the browser?
Check how Remix controls which variables are sent to the browser.
Remix only exposes environment variables prefixed with PUBLIC_ to client code. Variables without this prefix are undefined in the browser for security.
Which approach correctly shares environment variables safely between server and client in Remix?
Think about security and how Remix handles environment variables.
Remix requires prefixing variables with PUBLIC_ to expose them to client code. This keeps secrets safe on the server and only shares intended variables with the client.
Given the following Remix loader and component code, what will be rendered in the browser?
import { useLoaderData } from "@remix-run/react"; export async function loader() { return { apiKey: process.env.API_KEY }; } export default function Page() { const data = useLoaderData(); return <div>API Key: {data.apiKey ?? 'none'}</div>; }
Assume API_KEY is set in the environment and useLoaderData is imported correctly.
The loader returns the API key from process.env. The component uses useLoaderData to get this data and renders it. So the actual API key value is shown.