fix(api): resolve server-side relative URLs

This commit is contained in:
zetaloop
2026-02-28 16:56:46 +08:00
parent 536465aa54
commit bffd8b4968
+10 -1
View File
@@ -38,6 +38,15 @@ export async function httpJson<T>(path: string, init?: JsonRequestInit): Promise
throw new Error("Absolute URLs are not allowed") throw new Error("Absolute URLs are not allowed")
} }
let url = path
if (
typeof window === "undefined" &&
path.startsWith("/") &&
process.env.NEXT_PUBLIC_BACKEND_URL
) {
url = `${process.env.NEXT_PUBLIC_BACKEND_URL.replace(/\/$/, "")}${path}`
}
const { json, headers: headersInit, body: bodyInit, ...rest } = init ?? {} const { json, headers: headersInit, body: bodyInit, ...rest } = init ?? {}
const headers = new Headers(headersInit) const headers = new Headers(headersInit)
@@ -58,7 +67,7 @@ export async function httpJson<T>(path: string, init?: JsonRequestInit): Promise
if (xsrfToken) headers.set("XSRF-TOKEN", xsrfToken) if (xsrfToken) headers.set("XSRF-TOKEN", xsrfToken)
} }
const res = await fetch(path, { const res = await fetch(url, {
...rest, ...rest,
headers, headers,
body, body,