fix(api): resolve server-side relative URLs
This commit is contained in:
+10
-1
@@ -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,
|
||||||
|
|||||||
Reference in New Issue
Block a user