Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/web/src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import type { NativeApi, DesktopBridge } from "@peakcode/contracts";

interface ImportMetaEnv {
readonly APP_VERSION: string;
/** WebSocket server URL (set when server binds to a specific host). */
readonly VITE_WS_URL: string;
/** WebSocket server port (set when server binds to a wildcard address like 0.0.0.0). */
readonly VITE_WS_PORT: string;
}

interface ImportMeta {
Expand Down
5 changes: 4 additions & 1 deletion apps/web/src/wsTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ function makeSocketUrl(explicitUrl: string | null): string {
if (explicitUrl) return resolveRpcUrl(explicitUrl);
const bridgeUrl = window.desktopBridge?.getWsUrl();
const envUrl = import.meta.env.VITE_WS_URL as string | undefined;
// VITE_WS_PORT is set when the server binds to a wildcard address (0.0.0.0 / ::),
// so the browser constructs the WS URL from its own hostname + the correct server port.
const envWsPort = import.meta.env.VITE_WS_PORT as string | undefined;
const rawUrl =
bridgeUrl && bridgeUrl.length > 0
? bridgeUrl
: envUrl && envUrl.length > 0
? envUrl
: `${window.location.protocol === "https:" ? "wss" : "ws"}://${window.location.hostname}:${window.location.port}`;
: `${window.location.protocol === "https:" ? "wss" : "ws"}://${window.location.hostname}:${envWsPort && envWsPort.length > 0 ? envWsPort : window.location.port}`;
return resolveRpcUrl(rawUrl);
}

Expand Down
8 changes: 7 additions & 1 deletion apps/web/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { defineConfig } from "vite";
import pkg from "./package.json" with { type: "json" };

const port = Number(process.env.PORT ?? 5733);
const viteHost = process.env.PEAKCODE_HOST || "localhost";
const sourcemapEnv = process.env.PEAKCODE_WEB_SOURCEMAP?.trim().toLowerCase();

const buildSourcemap =
Expand Down Expand Up @@ -38,14 +39,19 @@ export default defineConfig({
],
},
define: {
// In dev mode, tell the web app where the WebSocket server lives
// In dev mode, tell the web app where the WebSocket server lives.
// When binding to a wildcard address (0.0.0.0 / ::), VITE_WS_URL is left
// unset and VITE_WS_PORT is injected instead so the browser can construct
// the WS URL from window.location.hostname + the correct port.
"import.meta.env.VITE_WS_URL": JSON.stringify(process.env.VITE_WS_URL ?? ""),
"import.meta.env.VITE_WS_PORT": JSON.stringify(process.env.VITE_WS_PORT ?? ""),
"import.meta.env.APP_VERSION": JSON.stringify(pkg.version),
},
resolve: {
tsconfigPaths: true,
},
server: {
host: viteHost,
port,
strictPort: true,
hmr: {
Expand Down
Loading