From a769df5d44d4c01fe5f3f6ef601dd9b09d5c32c2 Mon Sep 17 00:00:00 2001 From: mlogclub Date: Tue, 21 Apr 2026 23:08:40 +0800 Subject: [PATCH] fix(api): enhance createWebSocketBaseUrl function to handle development environment and window context --- dashboard/lib/api/websocket.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/dashboard/lib/api/websocket.ts b/dashboard/lib/api/websocket.ts index 659a4a4..b7c4b4d 100644 --- a/dashboard/lib/api/websocket.ts +++ b/dashboard/lib/api/websocket.ts @@ -1,6 +1,16 @@ const API_BASE_URL = - process.env.NEXT_PUBLIC_API_BASE_URL?.trim() || "http://127.0.0.1:8083" + process.env.NEXT_PUBLIC_API_BASE_URL?.trim() || + (process.env.NODE_ENV === "development" ? "http://127.0.0.1:8083" : "") export function createWebSocketBaseUrl() { - return API_BASE_URL.replace(/^http/, "ws").replace(/\/$/, "") + if (API_BASE_URL) { + return API_BASE_URL.replace(/^http/, "ws").replace(/\/$/, "") + } + + if (typeof window === "undefined") { + return "" + } + + const protocol = window.location.protocol === "https:" ? "wss:" : "ws:" + return `${protocol}//${window.location.host}` }