Files
Enterprise-Knowledge-Base/frontend/src/utils/api-base.ts
T
toy0116 05d35c904b 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.
2026-05-05 00:58:19 +08:00

10 lines
495 B
TypeScript

export function getApiBaseUrl(): string {
// 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;
}