feat(frontend): respect BASE_URL so the SPA works behind a path-prefix reverse proxy

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.
This commit is contained in:
toy0116
2026-05-02 10:13:48 +08:00
committed by lyingbug
parent e4fe9d7d9b
commit 05d35c904b
+7 -3
View File
@@ -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;
}