Environment Configuration Guide
Overview
The frontend uses environment-specific configuration to connect to different backends in development vs production.How It Works
Development (Local)
- File:
.env(not committed to git) - Backend:
http://localhost:8080 - Vite Proxy: Automatically proxies
/api/*requests to backend - Hot Reload: ✅ Enabled
Production (Deployed)
- File:
.env.production(committed to git) - Backend: Relative
/api(proxied tohttps://api.retailgrid.io) - Serving: Cloud Run frontend (nginx) serves static assets
- Proxy: Nginx forwards
/api/*to the backend
Environment Files
.env.local (Local Development - NOT in git)
VITE_API_URL empty
.env.cloud (Cloud Backend Template - IN git)
cp .env.cloud .env.local to connect to production backend
When to use: Testing frontend changes against production backend without running backend locally
.env.example (Template - IN git)
.env.local with custom configuration
.env.production (Production - IN git)
/api (proxied to https://api.retailgrid.io)
How Vite Handles Environment Variables
Variable Naming
- Must start with
VITE_to be exposed to frontend code - Example:
VITE_API_URL,VITE_APP_NAME - Non-VITE variables are only available during build, not in browser
Upload mode (direct-to-GCS resumable)
The upload page supports direct-to-GCS resumable uploads (recommended for Cloud Run) and can be toggled per environment:- Production builds (
import.meta.env.PROD === true): always use direct-to-GCS resumable uploads - Local dev: default to legacy backend multipart upload (simpler), unless you opt-in:
VITE_FORCE_RESUMABLE_UPLOAD=true, local dev will use the same resumable flow as production, but you must also:
- Run backend with real GCS enabled (
USE_LOCAL_STORAGE=false) and valid credentials - Ensure the uploads bucket has CORS allowing
http://localhost:3000forPUT+Content-Rangeheaders
Access in Code
Build-time vs Runtime
- Environment variables are baked into the build at compile time
- Changing
.envafter build has no effect - Must rebuild:
npm run build
Current Setup (Best Practices)
✅ What We’re Doing Right
-
Vite Proxy for Development
- Proxies
/api,/upload,/inputto backend - No CORS issues during development
- Works seamlessly with
npm run dev
- Proxies
-
Relative URLs in Production
- API client uses
baseURL: '/api' - No hardcoded backend URLs
- Works when frontend + backend are on same domain
- API client uses
-
Environment-Specific Configuration
.envfor local development (git-ignored).env.productionfor production (committed).env.exampleas template (committed)
-
Fallback to Sensible Defaults
Configuration Flow
Development Mode (npm run dev)
Production Mode (npm run build)
Common Scenarios
Scenario 1: Local Development with Local Backend (Default)
Use case: Running both frontend and backend locally Solution: Just run the dev server (no .env.local needed)http://localhost:8080
Scenario 2: Local Development with Cloud Backend
Use case: Testing frontend changes against production backend Solution: Copy cloud environment templatehttps://api.retailgrid.io
When to switch back to local backend:
Scenario 3: Backend on Different Port Locally
Problem: Your backend runs on port 5001 instead of 8080 Solution: Create/update.env.local
Scenario 4: Backend on Different Domain (Staging)
Problem: Testing against staging backend athttps://staging-api.example.com
Solution: Create .env.local with staging URL
Scenario 5: Production Deployment
Problem: Deploying to production Solution: Just build normally.env.production which points to the cloud backend.
Scenario 6: Docker Development
Problem: Backend is in Docker athttp://backend:8080
Solution: Update .env.local
Debugging
Check Current Environment Variables
Add this temporarily to any component:Check Proxy Configuration
In browser DevTools Network tab:- Request URL should show
http://localhost:3000/api/...(dev server) - Proxied to:
http://localhost:8080/api/...(backend)
Check Build Output
After build, checkdist/assets/index-*.js:
Best Practices Summary
- ✅ Never commit
.env- Add to.gitignore - ✅ Always commit
.env.example- For other developers - ✅ Use relative URLs in production - No hardcoded backend URLs
- ✅ Use Vite proxy in development - Avoids CORS issues
- ✅ Prefix with
VITE_- Only way to access in frontend code - ✅ Restart dev server - After changing
.env - ✅ Rebuild for production - After changing
.env.production - ✅ Use sensible defaults - Fallback if env var not set
- ✅ Document in
.env.example- Help other developers
Quick Reference
| Environment | File | Backend URL | Command |
|---|---|---|---|
| Local Dev (Local Backend) | .env.local (empty or not created) | http://localhost:8080 (via proxy) | npm run dev |
| Local Dev (Cloud Backend) | .env.local (copied from .env.cloud) | https://api.retailgrid.io | cp .env.cloud .env.local && npm run dev |
| Production | .env.production | /api (proxied to https://api.retailgrid.io) | npm run build |
Setup for New Developers
Option 1: Local Backend (Default)
http://localhost:8080 via Vite proxy
Option 2: Cloud Backend
https://api.retailgrid.io