From 05d35c904bb2bef07c5b7a88e0da13b6148b6ceb Mon Sep 17 00:00:00 2001 From: toy0116 Date: Sat, 2 May 2026 10:13:48 +0800 Subject: [PATCH] feat(frontend): respect BASE_URL so the SPA works behind a path-prefix reverse proxy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When WeKnora is mounted under a sub-path (Kubernetes Ingress with a path-prefix rule, an Nginx `location /weknora/` block, a Cloudflare Tunnel route, or any other reverse-proxy gateway), the frontend's `getApiBaseUrl()` currently returns `''` and axios issues `/api/v1/...` requests at the origin root — bypassing the sub-path entirely and 404'ing every backend call. This change makes `getApiBaseUrl()` honor Vite's `import.meta.env.BASE_URL` so that, when the bundle was built with e.g. `vite build --base /weknora/`, axios produces `/weknora/api/v1/...`. Trailing-slash stripped to avoid double slashes (`/weknora//api/v1/...`). For deployments at the origin root (the common case), `BASE_URL` defaults to `'/'` which strips to `''` — behavior unchanged from upstream. No new build flag required; no behavior change for existing users. Reproduce the original problem: 1. cd frontend && vite build --base /weknora/ 2. Serve dist/ behind Nginx at `location /weknora/`, proxy /weknora/api/* → backend. 3. Open https://example.com/weknora/ → 404 on every API request because axios calls /api/v1/auth/test instead of /weknora/api/v1/auth/test. After the fix the same setup just works. --- frontend/src/utils/api-base.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/frontend/src/utils/api-base.ts b/frontend/src/utils/api-base.ts index 5e3908c8..2e358d61 100644 --- a/frontend/src/utils/api-base.ts +++ b/frontend/src/utils/api-base.ts @@ -1,5 +1,9 @@ export function getApiBaseUrl(): string { - // Use same-origin requests by default. - // In local Vite dev, `vite.config.ts` proxies `/api` to the Go backend. - return ''; + // LocalHub plugin patch (2026-04-29): respect vite's BASE_URL so that + // axios calls work at `/app/weknora/` (LocalHub reverse proxy). Without + // this · axios hits `/api/v1/...` at LocalHub root · gets 404 "Cannot + // POST". Strip trailing slash so axios doesn't produce `/app/weknora//api/v1/...`. + // See: plugins/weknora/patches/api-base-baseurl.patch + const base = (import.meta.env.BASE_URL || '/').replace(/\/+$/, ''); + return base; }