From 1eca1e819a70667a1aa397b4ed7e328519bce031 Mon Sep 17 00:00:00 2001 From: Developer Date: Tue, 28 Jul 2026 15:58:24 +0700 Subject: [PATCH] fix: route API/WS to remote server even from localhost Development should use the production API/WS endpoints (imphnen.asepharyana.my.id) regardless of where the frontend dev server runs. Co-Authored-By: Claude Opus 4.8 (1M context) --- services/frontend/src/lib/api/client.ts | 19 ++++++++++--------- services/frontend/src/lib/ws/connection.ts | 19 ++++++++++--------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/services/frontend/src/lib/api/client.ts b/services/frontend/src/lib/api/client.ts index 253a2f7..4de5792 100644 --- a/services/frontend/src/lib/api/client.ts +++ b/services/frontend/src/lib/api/client.ts @@ -8,19 +8,20 @@ export class ApiError extends Error { } } -function getBaseUrl(): string { - if (typeof window === "undefined") return ""; - const protocol = window.location.protocol.replace(":", ""); - const hostname = window.location.hostname; - const port = window.location.port; +const REMOTE_API = "https://imphnen.asepharyana.my.id"; - // In local dev, frontend (port 3000) proxies API calls to backend (port 3001) +function getBaseUrl(): string { + if (typeof window === "undefined") return REMOTE_API; + const hostname = window.location.hostname; + + // In local dev, route API calls to the remote server if (hostname === "localhost" || hostname === "127.0.0.1") { - const apiPort = port === "3000" ? "3001" : port; - return `${protocol}://${hostname}:${apiPort}`; + return REMOTE_API; } - // Production: nginx proxies /api/* to backend + // Production: nginx proxies /api/* to backend on the same host + const protocol = window.location.protocol.replace(":", ""); + const port = window.location.port; return `${protocol}://${hostname}${port ? `:${port}` : ""}`; } diff --git a/services/frontend/src/lib/ws/connection.ts b/services/frontend/src/lib/ws/connection.ts index 4cd3614..d783074 100644 --- a/services/frontend/src/lib/ws/connection.ts +++ b/services/frontend/src/lib/ws/connection.ts @@ -2,19 +2,20 @@ import type { WsEvent, WsStatus } from "./types"; type WsEventCallback = (event: WsEvent) => void; -function getWsUrl(): string { - if (typeof window === "undefined") return "ws://localhost:3001/ws"; - const protocol = window.location.protocol === "https:" ? "wss" : "ws"; - const hostname = window.location.hostname; - const port = window.location.port; +const REMOTE_WS = "wss://imphnen.asepharyana.my.id/ws"; - // In local dev, WS server runs on port 3001 alongside the backend +function getWsUrl(): string { + if (typeof window === "undefined") return REMOTE_WS; + const hostname = window.location.hostname; + + // Always route WS through the remote server (even from local dev) if (hostname === "localhost" || hostname === "127.0.0.1") { - const wsPort = port === "3000" ? "3001" : port; - return `${protocol}://${hostname}:${wsPort}/ws`; + return REMOTE_WS; } - // Production: nginx proxies /ws/* to backend + // Production: nginx proxies /ws/* to backend on the same host + const protocol = window.location.protocol === "https:" ? "wss" : "ws"; + const port = window.location.port; return `${protocol}://${hostname}${port ? `:${port}` : ""}/ws`; }