Skip to content

Commit cce7c0d

Browse files
committed
fix(web): use current origin for dev websocket auth
1 parent cb65342 commit cce7c0d

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

packages/web/src/ws/__tests__/client.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,17 @@ describe("web WsClient", () => {
13131313
expect(resolveWsUrl()).toBe("ws://127.0.0.1:43173/ws");
13141314
});
13151315

1316+
it("uses the current browser origin in development when no backend URL is configured", () => {
1317+
vi.stubGlobal("window", {
1318+
location: {
1319+
protocol: "http:",
1320+
host: "localhost:5173",
1321+
},
1322+
});
1323+
1324+
expect(resolveWsUrl()).toBe("ws://localhost:5173/ws");
1325+
});
1326+
13161327
it("falls back to the local backend URL when window is unavailable", () => {
13171328
vi.stubGlobal("window", undefined);
13181329

packages/web/src/ws/client.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -688,17 +688,14 @@ export class WsClient {
688688

689689
/**
690690
* Resolve WebSocket URL based on current location
691-
* In development, connect directly to backend server
692691
*/
693692
export function resolveWsUrl(): string {
694693
const configuredUrl = import.meta.env.VITE_BACKEND_WS_URL;
695694
if (configuredUrl) {
696695
return normalizeWsUrl(configuredUrl);
697696
}
698697

699-
// Development and test environments connect directly to the backend, and
700-
// node-based tests may not provide a browser location object at all.
701-
if (import.meta.env.DEV || import.meta.env.MODE === "test" || typeof window === "undefined") {
698+
if (typeof window === "undefined") {
702699
return "ws://127.0.0.1:4173/ws";
703700
}
704701
const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";

0 commit comments

Comments
 (0)