Build frontend¶
This page explains how to build and run the React frontend outside of Docker, in order to benefit from Vite's Hot Module Replacement (HMR) during development.
Target stack¶
- React 18
- Vite 5
- Node.js 20 LTS
See Prerequisites for exact versions.
Installing dependencies¶
From the repository root:
The first install can take 30 to 90 seconds depending on your connection. The package-lock.json file is committed to guarantee reproducible installs.
Starting the Vite dev server¶
Vite starts a development server on port 5173 (the default Vite port). It exposes a URL:
Open this URL in your browser. Code changes are automatically reflected via Hot Module Replacement — the page updates without a full reload and preserves React state when possible.
Configuration via Vite environment variables¶
The frontend uses two main environment variables:
| Variable | Default | Role |
|---|---|---|
VITE_API_URL |
https://perfshop-api.perfshop.io |
Backend URL |
VITE_LANG |
fr |
Active language (fr or en) |
For local development, point VITE_API_URL at your local backend:
Unix / macOS¶
Windows PowerShell¶
.env.local file¶
A more comfortable alternative: create a frontend/.env.local file which Vite reads automatically at startup. This file is in .gitignore by default and is never committed:
Production build¶
Vite compiles the application into optimized static assets in frontend/dist/:
- Minified and tree-shaken JavaScript
- Extracted and minified CSS
- Images copied with a content hash for cache-busting
- HTML index with updated
<script>and<link>references
You can preview the production build locally:
This starts a lightweight static server that serves dist/ on a random port — useful to validate that a build optimization has not broken the application.
Runtime injection of variables¶
The frontend Dockerfile builds the image only once, with default values baked into the bundle (VITE_API_URL=http://localhost:9080, VITE_MONITORING_URL=http://localhost:3001, VITE_LANG=fr, recorded in the image's BAKED_* variables). When the container starts, the Nginx entrypoint runs env-inject.sh (copied into /docker-entrypoint.d/): it replaces the baked URLs in the JS files with the values of the environment variables, and writes the language into the <html lang> of index.html (baked from %VITE_LANG%), which the application reads before the baked value (src/i18n/lang.js).
# excerpt from frontend/env-inject.sh
sed -i -e "s|${BAKED_API_URL}|${RUNTIME_API_URL}|g" \
-e "s|${BAKED_MONITORING_URL}|${RUNTIME_MONITORING_URL}|g" "$file" # every .js file of the bundle
sed -i "s|lang=\"${BAKED_LANG}\"|lang=\"${RUNTIME_LANG}\"|g" "$INDEX_FILE" # index.html
The language is never replaced in the bundle code
Replacing every "fr" with "en" in the JS also rewrote case "fr": return "fr-FR" into case "en": return "fr-FR": the shop deployed in English displayed its amounts in French format ("9,90 €"). Fixed on 2026-10-01; test: frontend/test/langue-deploiement.test.mjs.
This allows publishing a single image and deploying it to multiple environments (dev, staging, prod, NAS, VPS) by simply changing the container's environment variables. No per-target rebuild needed.
In npm run dev mode, this mechanism is not used — Vite injects the variables directly at build time.
CORS¶
When you run the frontend on localhost:5173 and the backend on localhost:8080, the browser considers them different origins and applies the CORS policy. The backend must therefore explicitly allow the origin of the Vite dev server.
The backend's CORS_ALLOWED_ORIGINS environment variable lists the allowed origins. By default it contains http://localhost:8090 (the port exposed by Docker Compose) but not http://localhost:5173 (the Vite dev server). Add it:
Then restart the backend.
This applies to a backend started outside a container. In a container, the compose files build this list from PUBLIC_FRONTEND_URL, PUBLIC_MONITORING_URL, PUBLIC_CHAOS_URL and PUBLIC_ADMIN_URL: no .env key extends it — use the Vite proxy below.
Vite proxy configuration (alternative)¶
Instead of allowing CORS on the backend side, you can configure a proxy in vite.config.js that routes the dev server's /api/* calls to the local backend:
export default defineConfig({
server: {
proxy: {
'/api': {
target: 'http://localhost:8080',
changeOrigin: true,
},
},
},
})
In this mode, the browser sees all requests coming from localhost:5173, so no CORS. This is the simplest method if you do not want to touch the backend configuration.
Source code structure¶
frontend/src/
├── main.jsx ← React entry point, mounts <I18nProvider> + <App>
├── App.jsx ← Routes, AppShell, mounts PedagogiqueOrchestrator
├── index.css ← Global styles
├── App.css ← App-specific styles
├── chaos-agent.js ← Frontend Chaos on the browser side (side effects at load)
├── pages/ ← Page components
│ ├── Home.jsx
│ ├── Products.jsx
│ ├── ProductDetail.jsx
│ ├── Cart.jsx
│ ├── Checkout.jsx
│ ├── Login.jsx
│ ├── Register.jsx
│ ├── Profile.jsx
│ ├── MyOrders.jsx
│ ├── OrderConfirmation.jsx
│ ├── AdminPortal.jsx
│ ├── PedagogiqueOrchestrator.jsx
│ ├── PedagogiqueOverlay.jsx
│ ├── PedagogiqueTimer.jsx
│ ├── usePedagogiqueState.js
│ ├── SearchBar.jsx
│ └── CategorySidebar.jsx
├── pedagogique/succes/
│ └── PedagogiqueSucces.jsx ← Standalone /s/:token page
├── services/
│ └── api.js ← Single API client (all fetch calls go through here)
└── i18n/
├── I18nContext.jsx ← Provider + useT hook
├── fr.json ← French dictionary (~400 keys)
└── en.json ← English dictionary
Testing the Docker build locally¶
To build the frontend image without Compose:
cd frontend
docker build -t perfshop-frontend:local .
docker run --rm -p 8090:80 \
-e VITE_API_URL=http://host.docker.internal:8080 \
-e VITE_LANG=fr \
perfshop-frontend:local
Then open http://localhost:8090 to validate that runtime injection works correctly.
See also¶
- E-commerce frontend — architecture and role of the frontend
- Build backend — for the Spring Boot side
- Running Docker Compose — everything containerized