From 41a6391ce678630999a64e9059013c0a62e7e410 Mon Sep 17 00:00:00 2001 From: kongkx Date: Mon, 31 Aug 2026 00:36:52 +0800 Subject: [PATCH] fix: handle empty BASE_URL in buildUrl to prevent Invalid URL error When VITE_API_BASE_URL is unset, BASE_URL defaults to '', which causes new URL(path, '') to throw TypeError: Invalid URL. Fall back to location.origin (browser/jsdom) or 'http://localhost' (SSR). --- src/api/client.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/api/client.ts b/src/api/client.ts index 73713e2..2a1dc0f 100644 --- a/src/api/client.ts +++ b/src/api/client.ts @@ -28,7 +28,8 @@ interface RequestOptions { } function buildUrl(path: string, params?: ListParams): string { - const url = new URL(path, BASE_URL) + const base = BASE_URL || (typeof location !== 'undefined' ? location.origin : 'http://localhost') + const url = new URL(path, base) if (params) { for (const [key, value] of Object.entries(params)) { if (value !== undefined && value !== null && value !== '') {