Performance: Environment variable management
This affects page load speed and runtime performance by controlling how environment variables are injected and accessed in the app bundle and server.
export const loader = () => {
return json({
ENV: {
PUBLIC_API_URL: process.env.PUBLIC_API_URL
}
});
};
// Client-side
const { ENV } = useLoaderData();
console.log(ENV.PUBLIC_API_URL);export const loader = () => {
return json({
ENV: process.env
});
};
// Client-side
const { ENV } = useLoaderData();
console.log(ENV);| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Exposing full process.env to client | N/A | 0 | High due to large bundle parsing | [X] Bad |
| Exposing only needed public env vars | N/A | 0 | Low due to smaller bundle | [OK] Good |
API_KEY in a Remix loader function?process.env.VARIABLE_NAME.process.env.API_KEY, which is correct. Others use invalid or client-only patterns.apiUrl if process.env.API_URL is undefined?
export async function loader() {
const apiUrl = process.env.API_URL || 'https://default.api.com';
return { apiUrl };
}process.env.API_URL is undefined (falsy), the expression returns the right side string.process.env.API_URL is undefined, apiUrl becomes 'https://default.api.com'.export function loader() {
return { apiKey: process.env.SECRET_API_KEY };
}
export default function Component() {
const { apiKey } = useLoaderData();
return <div>API Key: {apiKey}</div>;
}PUBLIC_ to client code safely; secrets remain server-only.PUBLIC_ and access them in client code; keep secrets without prefix and access only in server code follows this pattern, separating public and secret keys correctly. Others expose secrets or use insecure methods.Pass the test and this pattern moves unknown