diff --git a/apps/web/src/lib/utils.ts b/apps/web/src/lib/utils.ts index e48f815461..280dc72f2c 100644 --- a/apps/web/src/lib/utils.ts +++ b/apps/web/src/lib/utils.ts @@ -52,13 +52,19 @@ export const resolveServerUrl = (options?: { pathname?: string | undefined; searchParams?: Record | undefined; }): string => { - const rawUrl = firstNonEmptyString( + let rawUrl = firstNonEmptyString( options?.url, window.desktopBridge?.getWsUrl(), import.meta.env.VITE_WS_URL, window.location.origin, ); + // When accessing from a remote host (e.g. mobile), replace localhost in the + // resolved URL with the actual page hostname so the connection reaches the server. + if (rawUrl.includes("localhost") && window.location.hostname !== "localhost") { + rawUrl = rawUrl.replace("localhost", window.location.hostname); + } + const parsedUrl = new URL(rawUrl); if (options?.protocol) { parsedUrl.protocol = options.protocol; diff --git a/apps/web/vite.config.ts b/apps/web/vite.config.ts index 56b138d331..96deb57019 100644 --- a/apps/web/vite.config.ts +++ b/apps/web/vite.config.ts @@ -6,6 +6,7 @@ import { defineConfig } from "vite"; import pkg from "./package.json" with { type: "json" }; const port = Number(process.env.PORT ?? 5733); +const host = process.env.T3CODE_HOST ?? "localhost"; const sourcemapEnv = process.env.T3CODE_WEB_SOURCEMAP?.trim().toLowerCase(); const buildSourcemap = @@ -42,14 +43,12 @@ export default defineConfig({ }, server: { port, + host, strictPort: true, - hmr: { - // Explicit config so Vite's HMR WebSocket connects reliably - // inside Electron's BrowserWindow. Vite 8 uses console.debug for - // connection logs — enable "Verbose" in DevTools to see them. - protocol: "ws", - host: "localhost", - }, + hmr: + host === "localhost" + ? { protocol: "ws", host: "localhost" } + : { protocol: "ws" }, }, build: { outDir: "dist", diff --git a/turbo.json b/turbo.json index 9359376f59..a50b647d65 100644 --- a/turbo.json +++ b/turbo.json @@ -11,7 +11,8 @@ "T3CODE_NO_BROWSER", "T3CODE_HOME", "T3CODE_AUTH_TOKEN", - "T3CODE_DESKTOP_WS_URL" + "T3CODE_DESKTOP_WS_URL", + "T3CODE_HOST" ], "tasks": { "build": {