diff --git a/README.md b/README.md index 7800c7d..35dd0b9 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ bun run dev Open [http://localhost:5173](http://localhost:5173). -The Vite server proxies `/auth`, `/users`, `/notes`, `/events`, `/exams`, `/exam-room`, `/time`, and `/health` to the backend so the HttpOnly session cookie stays same-origin. +The Vite server proxies `/api/auth`, `/api/users`, `/api/notes`, `/api/events`, `/api/exams`, `/api/courses`, `/api/marks`, `/api/time`, and `/api/health` to the backend so the HttpOnly session cookie stays same-origin while page URLs like `/notes` remain frontend routes on refresh. ## Scripts diff --git a/nginx.conf b/nginx.conf index 3396a1a..b83e326 100644 --- a/nginx.conf +++ b/nginx.conf @@ -3,11 +3,8 @@ server { root /usr/share/nginx/html; index index.html; - location / { - try_files $uri /index.html; - } - - location ~ ^/(auth|users|notes|events|exams|courses|marks)(/|$) { + location ~ ^/api/(auth|users|notes|events|exams|courses|marks|health|time)(/|$) { + rewrite ^/api(/.*)$ $1 break; proxy_pass http://hezarfen-backend:8080; proxy_http_version 1.1; proxy_set_header Host $http_host; @@ -18,11 +15,17 @@ server { proxy_set_header Connection "upgrade"; } - location ~ ^/(health|time)$ { - proxy_pass http://hezarfen-backend:8080; - proxy_set_header Host $http_host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto $scheme; + location /assets/ { + try_files $uri =404; + add_header Cache-Control "public, max-age=31536000, immutable" always; + } + + location = /index.html { + add_header Cache-Control "no-store" always; + } + + location / { + try_files $uri /index.html; + add_header Cache-Control "no-store" always; } } diff --git a/src/api/client.ts b/src/api/client.ts index 8a71729..437490e 100644 --- a/src/api/client.ts +++ b/src/api/client.ts @@ -16,6 +16,8 @@ type RequestOptions = { signal?: AbortSignal; }; +const API_PREFIX = "/api"; + export async function client(path: string, options: RequestOptions = {}): Promise { const headers: Record = {}; let body: string | undefined; @@ -25,7 +27,7 @@ export async function client(path: string, options: RequestOptions = {}): Pro body = JSON.stringify(options.body); } - const res = await fetch(path, { + const res = await fetch(`${API_PREFIX}${path}`, { method: options.method ?? "GET", headers, body, diff --git a/src/components/exams/exam-room-ws.tsx b/src/components/exams/exam-room-ws.tsx index 28827e8..7441356 100644 --- a/src/components/exams/exam-room-ws.tsx +++ b/src/components/exams/exam-room-ws.tsx @@ -49,7 +49,7 @@ export function ExamRoomWS(props: { exam: Exam }) { const wsUrl = () => { const proto = window.location.protocol === "https:" ? "wss:" : "ws:"; - return `${proto}//${window.location.host}/exams/${encodeURIComponent(props.exam.id)}/attempt/ws`; + return `${proto}//${window.location.host}/api/exams/${encodeURIComponent(props.exam.id)}/attempt/ws`; }; const connectWs = () => { diff --git a/vite.config.ts b/vite.config.ts index 4834faf..790f655 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -12,12 +12,14 @@ export default defineConfig({ }, }, server: { - proxy: Object.fromEntries( - ["/auth", "/users", "/notes", "/events", "/exams", "/courses", "/marks", "/health", "/time"].map((p) => [ - p, - { target, changeOrigin: true }, - ]), - ), + proxy: { + "/api": { + target, + changeOrigin: true, + ws: true, + rewrite: (path) => path.replace(/^\/api(?=\/|$)/, ""), + }, + }, }, build: { target: "esnext",