Skip to content

Commit 4603e90

Browse files
authored
fix: proxy /api/* from Vite dev server to the backend (closes localhost:5173 health-check 'Unexpected token <' error) (#65)
1 parent a16fca2 commit 4603e90

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

docker-compose.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ services:
3030
- "5173:5173"
3131
depends_on:
3232
- app
33+
environment:
34+
# Vite dev-server proxy target. Inside the compose network, the
35+
# backend service is reachable at http://app:8000. The browser
36+
# still hits http://localhost:5173/api/v1/* and Vite proxies it.
37+
- VITE_API_PROXY_TARGET=http://app:8000
3338
# Bind-mount the source so Vite HMR sees host edits. node_modules stays
3439
# inside the container so platform-native deps aren't shadowed.
3540
volumes:

frontend/vite.config.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
import { defineConfig } from 'vite'
22
import react from '@vitejs/plugin-react'
33

4+
// `/api/*` calls from the React app are proxied to the FastAPI backend so
5+
// the dev experience matches a same-origin production deployment. Without
6+
// this Vite serves `index.html` for `/api/*`, which the browser then tries
7+
// to JSON.parse — producing "Unexpected token '<'".
8+
//
9+
// Override the target via env when needed:
10+
// - Local (no Docker): defaults to http://localhost:8000.
11+
// - docker-compose: set VITE_API_PROXY_TARGET=http://app:8000 (inside
12+
// the compose network the backend hostname is `app`).
13+
const API_PROXY_TARGET = process.env.VITE_API_PROXY_TARGET ?? 'http://localhost:8000'
14+
415
// https://vite.dev/config/
516
export default defineConfig({
617
plugins: [react()],
18+
server: {
19+
proxy: {
20+
'/api': {
21+
target: API_PROXY_TARGET,
22+
changeOrigin: true,
23+
},
24+
},
25+
},
726
})

0 commit comments

Comments
 (0)