fix(web): default API_BASE_URL to same origin, not localhost:3000

Found live on http://batch.dev.kyuno.fr/: every API call went to
http://localhost:3000 in the visitor's own browser instead of the
deployed origin, because API_BASE_URL fell back to a hardcoded
"http://localhost:3000" whenever VITE_API_URL wasn't set at build time
— which is exactly the case for the production Docker image (apps/web/.env
is excluded by .dockerignore, so Vite never sees VITE_API_URL there).

Now defaults to "" (same origin as the page), correct for the merged
single-container deployment where the API serves this very frontend
build. Native dev (pnpm dev:web) is unaffected — apps/web/.env still sets
VITE_API_URL=http://localhost:3000 explicitly there.

Verified: built the Docker image, confirmed "localhost:3000" no longer
appears in the bundled JS, and docker compose up shows /auth/me called
against the container's own origin.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Nicolas 2026-08-17 23:28:24 +02:00
parent 6fec167e86
commit 44e9245a0f

View file

@ -12,8 +12,16 @@ import {
type ThemePreference,
} from "@batch-cooking/shared";
/** Base URL of the API, configurable via `VITE_API_URL` (see `.env.example`). */
const API_BASE_URL: string = import.meta.env.VITE_API_URL ?? "http://localhost:3000";
/**
* Base URL of the API, configurable via `VITE_API_URL` (see `.env.example`).
* Defaults to `""` (same origin as the page) correct for the production
* Docker image, where the API serves this very frontend build (see
* apps/api/Dockerfile), so a relative path already reaches it. Native dev
* (`pnpm dev:web`) overrides this via `VITE_API_URL=http://localhost:3000`
* in `apps/web/.env`, since the Vite dev server (5173) and the API (3000)
* are on different origins there.
*/
const API_BASE_URL: string = import.meta.env.VITE_API_URL ?? "";
/**
* Thrown by {@link ApiClient} whenever the API responds with a non-2xx