Basic
Using Environment Variables with Vite
- Create a .env file in the root directory.
VITE_API_URL=https://api.example.com
- Access the variables in your code
const apiUrl = import.meta.env.VITE_API_URL;
console.log(apiUrl);
- Do Not Store Secrets Client-Side
- Environment variables are embedded into your JavaScript bundle during the build process, meaning they are publicly accessible in the browser
- Never store sensitive secrets like database passwords or private API keys meant for server-side use in your React frontend.
Redirect
- for SPAs, add add a _redirects file in the root directory
- This ensures allows router to handle the actual path logic (avoiding 404 when reloading)
/* /index.html 200