From bffd8b4968d714930b2aba045c7f676fe642c490 Mon Sep 17 00:00:00 2001 From: zetaloop Date: Sat, 28 Feb 2026 16:56:46 +0800 Subject: [PATCH] fix(api): resolve server-side relative URLs --- lib/api/http.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/api/http.ts b/lib/api/http.ts index ccefd91..0d61260 100644 --- a/lib/api/http.ts +++ b/lib/api/http.ts @@ -38,6 +38,15 @@ export async function httpJson(path: string, init?: JsonRequestInit): Promise 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 headers = new Headers(headersInit) @@ -58,7 +67,7 @@ export async function httpJson(path: string, init?: JsonRequestInit): Promise if (xsrfToken) headers.set("XSRF-TOKEN", xsrfToken) } - const res = await fetch(path, { + const res = await fetch(url, { ...rest, headers, body,