From f3d1ddad5d89d2554052ff65cf98d51be7b1b021 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 2 Apr 2026 18:23:58 +0000 Subject: [PATCH] Fix API URL config: use Next.js rewrite proxy instead of NEXT_PUBLIC_* NEXT_PUBLIC_* is baked at build time so runtime docker-compose environment vars have no effect. Instead: - next.config.ts rewrites /api/* -> API_BASE_URL/api/* at the server level - api.ts uses relative paths (no env var in client bundle at all) - docker-compose passes API_BASE_URL=http://cvfs-backend:8080 at runtime - Dockerfile no longer needs any build args Requests: browser -> Next.js server (/api/*) -> cvfs-backend:8080 (/api/*) https://claude.ai/code/session_01Xmxm2QLgFBgRJyYD6VukR6 --- apps/webapp/next.config.ts | 4 ++++ apps/webapp/src/libs/api.ts | 5 +++-- docker-compose.yml | 4 ++-- docker/webapp.Dockerfile | 2 -- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/apps/webapp/next.config.ts b/apps/webapp/next.config.ts index cb0278c..740aeb0 100644 --- a/apps/webapp/next.config.ts +++ b/apps/webapp/next.config.ts @@ -3,6 +3,10 @@ import path from "node:path"; const nextConfig: NextConfig = { outputFileTracingRoot: path.join(process.cwd(), "../.."), + async rewrites() { + const backend = process.env.API_BASE_URL ?? "http://localhost:9812"; + return [{ source: "/api/:path*", destination: `${backend}/api/:path*` }]; + }, }; export default nextConfig; diff --git a/apps/webapp/src/libs/api.ts b/apps/webapp/src/libs/api.ts index e3f17af..926b442 100644 --- a/apps/webapp/src/libs/api.ts +++ b/apps/webapp/src/libs/api.ts @@ -1,4 +1,6 @@ -const API = process.env.NEXT_PUBLIC_API_BASE_URL ?? "http://localhost:9812"; +// Empty base: all API calls go to /api/* which Next.js rewrites to the backend. +// The actual backend URL is set via API_BASE_URL env var in next.config.ts (server-side, runtime). +const API = ""; export type StructuredBlock = { path: string; @@ -130,4 +132,3 @@ export async function publishVersion( }); } -export { API as API_BASE_URL }; diff --git a/docker-compose.yml b/docker-compose.yml index 4b1b219..f0d4128 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,8 +11,8 @@ services: build: context: ./ dockerfile: ./docker/webapp.Dockerfile - args: - - NEXT_PUBLIC_API_BASE_URL=https://api.cv.alves.world + environment: + - API_BASE_URL=http://cvfs-backend:8080 networks: - dokploy-network - cvfs-network diff --git a/docker/webapp.Dockerfile b/docker/webapp.Dockerfile index ff14edb..cfddcce 100644 --- a/docker/webapp.Dockerfile +++ b/docker/webapp.Dockerfile @@ -8,8 +8,6 @@ COPY apps/webapp/package.json apps/webapp/bun.lock ./ RUN bun install --frozen-lockfile FROM deps AS builder -ARG NEXT_PUBLIC_API_BASE_URL=http://localhost:9812 -ENV NEXT_PUBLIC_API_BASE_URL=$NEXT_PUBLIC_API_BASE_URL COPY apps/webapp ./ RUN bun run build