Performance: Environment variables management
This affects page load speed and bundle size by controlling which variables are included in the build and exposed to the client.
// Only expose variables prefixed with VITE_ in .env files const apiKey = import.meta.env.VITE_API_KEY; // Keep secrets only on server side, not imported in client code console.log(apiKey);
// Using many environment variables directly in client code without filtering const apiKey = import.meta.env.VITE_API_KEY; const secretKey = import.meta.env.VITE_SECRET_KEY; // accidentally exposed secret console.log(apiKey, secretKey);
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Exposing all env variables including secrets | 0 (no DOM impact) | 0 | 0 | [X] Bad |
| Exposing only VITE_ prefixed variables | 0 (no DOM impact) | 0 | 0 | [OK] Good |
VITE_?VITE_ to avoid leaking sensitive data.VITE_ to the client code. correctly states this behavior. Other options are incorrect or unrelated.VITE_ to the client code. -> Option DVITE_ prefix = A [OK]VITE_API_URL inside a Vue component?import.meta.env.import.meta.env.VITE_API_URL, which is correct. Others use outdated or incorrect patterns.console.log(import.meta.env.VITE_APP_NAME);
.env contains:VITE_APP_NAME=MyVueApp
VITE_APP_NAME is defined in the .env file with value "MyVueApp".import.meta.env.VITE_APP_NAME with the value from the environment file at build time.console.log(import.meta.env.API_KEY);
undefined. What is the most likely reason?VITE_ to client code.API_KEY lacks the VITE_ prefix, so it is not exposed and returns undefined.VITE_ prefix. -> Option B.env.development
VITE_API_URL=https://dev.api.com
.env.production
VITE_API_URL=https://prod.api.com
.env.development or .env.production.import.meta.env.VITE_API_URL accesses the correct URL depending on the mode without manual checks.import.meta.env.VITE_API_URL which automatically picks the right file based on build mode. -> Option APass the test and this pattern moves unknown