fix: http client URL 构造必须统一加 /api/v1 前缀
公开页调用 /matches(无 /api/v1 前缀),被 nginx 当 SPA 回退返回 index.html, 前端 JSON 解析报 "Unexpected token <"。 修复:build_url 函数确保所有相对路径统一走 /api/v1, 已含前缀或绝对 URL 保持不变。 首页加载比赛恢复正常。
This commit is contained in:
@@ -17,6 +17,14 @@ export const UNAUTHORIZED_EVENT = 'profeto:unauthorized'
|
||||
const API_BASE = '/api/v1'
|
||||
const DEFAULT_TIMEOUT = 30_000
|
||||
|
||||
/** 所有 API 路径统一走 /api/v1,避免浏览器直接请求 /matches 被 nginx 当 SPA 回退 */
|
||||
function build_url(path: string): string {
|
||||
if (path.startsWith('http')) return path
|
||||
if (path.startsWith(API_BASE)) return path
|
||||
if (path.startsWith('/')) return `${API_BASE}${path}`
|
||||
return `${API_BASE}/${path}`
|
||||
}
|
||||
|
||||
export class ApiError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
@@ -37,7 +45,7 @@ interface RequestOptions {
|
||||
}
|
||||
|
||||
async function request<T>(path: string, options: RequestOptions = {}): Promise<T> {
|
||||
const url = path.startsWith('http') ? path : path.startsWith('/') ? path : `${API_BASE}${path}`
|
||||
const url = build_url(path)
|
||||
const { timeoutMs = DEFAULT_TIMEOUT, skipAuthHandling, signal } = options
|
||||
|
||||
const controller = new AbortController()
|
||||
|
||||
Reference in New Issue
Block a user