Files
OpenFlare/frontend/lib/services/core/config.ts
T
ryan 9f15f42b47 fix(frontend): keep browser API on same-origin rewrites in dev
Document that NEXT_PUBLIC_WAVELET_BACKEND_URL must stay unset for local
next dev so axios hits /api/* and Next rewrites proxy to the backend.
2026-08-03 21:25:22 +08:00

36 lines
996 B
TypeScript

/**
* API 配置
*
* 浏览器默认走同源相对路径(`/api/...`),由 `next.config` rewrites 反代到后端,
* 避免 dev 下跨域。仅当设置了 `NEXT_PUBLIC_WAVELET_BACKEND_URL` 时浏览器才直连后端
*(静态导出 embed / WebSocket 等场景)。
*/
/**
* 获取 API 基础 URL
* @returns API 基础 URL;浏览器侧空字符串表示同源相对路径
*/
export function getApiBaseUrl(): string {
if (typeof window === 'undefined') {
return (
process.env.WAVELET_BACKEND_URL ||
process.env.NEXT_PUBLIC_WAVELET_BACKEND_URL ||
'http://localhost:8000'
);
}
// 空字符串 → axios 请求 `/api/...`,经 Next rewrites 到后端,无 CORS 问题
return process.env.NEXT_PUBLIC_WAVELET_BACKEND_URL || '';
}
/**
* API 配置选项
*/
export const apiConfig = {
/** Basic URL */
baseURL: getApiBaseUrl(),
/** 超时时间(毫秒) */
timeout: 15000,
/** 携带凭证 */
withCredentials: true,
} as const;