Skip to content

Commit affbe2a

Browse files
authored
Merge pull request #34 from LinMoQC/fix/web-docker-standalone
🐛 fix(web): default API_BASE to /api/v1 when NEXT_PUBLIC is empty in …
2 parents d1551bc + 4a5fdef commit affbe2a

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

apps/web/src/lib/constants.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@
88

99
/**
1010
* 浏览器端 API 基础地址(NEXT_PUBLIC_*,构建时注入,面向用户浏览器)
11-
* Docker 部署时应设置为宿主机可访问的地址,如 http://localhost:8000/api/v1
11+
* - 开发:默认 http://localhost:8000/api/v1(也可用 .env.local 覆盖)
12+
* - 生产构建:若环境变量未设或为空字符串,默认 `/api/v1`(同域 + 反代 /api → 后端)
13+
* 避免出现空 baseURL 时 axios 把 `/setup/status` 发到页面源站根路径导致 404。
1214
*/
13-
export const API_BASE =
14-
process.env.NEXT_PUBLIC_API_BASE_URL ?? "http://localhost:8000/api/v1"
15+
export const API_BASE = (() => {
16+
const raw = process.env.NEXT_PUBLIC_API_BASE_URL?.trim() ?? ""
17+
if (raw !== "") return raw
18+
return process.env.NODE_ENV === "production" ? "/api/v1" : "http://localhost:8000/api/v1"
19+
})()
1520

1621
/**
1722
* 服务端 SSR 内部 API 地址(运行时读取,非 NEXT_PUBLIC_*)

docker-compose.prod.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ services:
128128
image: ghcr.io/linmoqc/lyranote/web:main
129129
restart: unless-stopped
130130
environment:
131+
# 仅同域反代时镜像内已是 /api/v1 即可。注意:NEXT_PUBLIC_* 在 next build 时写入产物;
132+
# 若 API 独立域名,须用该 URL 重新构建 web 镜像(非仅改此处)。
131133
NEXT_PUBLIC_API_BASE_URL: /api/v1
132134
INTERNAL_API_BASE_URL: http://api:8000/api/v1
133135
NODE_ENV: production

0 commit comments

Comments
 (0)