From 53d4c438edd987eb85e3f637ef0c012291018f8f Mon Sep 17 00:00:00 2001 From: Masen Furer Date: Mon, 6 Nov 2023 12:06:07 -0800 Subject: [PATCH] Assume wss:// with no port when frontend is HTTPS (#2129) --- reflex/.templates/web/utils/state.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/reflex/.templates/web/utils/state.js b/reflex/.templates/web/utils/state.js index 2a41bc319..6ffe72ded 100644 --- a/reflex/.templates/web/utils/state.js +++ b/reflex/.templates/web/utils/state.js @@ -12,6 +12,9 @@ import { initialEvents } from "utils/context.js" const EVENTURL = env.EVENT const UPLOADURL = env.UPLOAD +// These hostnames indicate that the backend and frontend are reachable via the same domain. +const SAME_DOMAIN_HOSTNAMES = ["localhost", "0.0.0.0", "::", "0:0:0:0:0:0:0:0"] + // Global variable to hold the token. let token; @@ -74,12 +77,13 @@ export const getToken = () => { export const getEventURL = () => { // Get backend URL object from the endpoint. const endpoint = new URL(EVENTURL); - if (endpoint.hostname === "localhost") { - // If the backend URL references localhost, and the frontend is not on localhost, - // then use the frontend host. + if (SAME_DOMAIN_HOSTNAMES.includes(endpoint.hostname)) { + // Use the frontend domain to access the backend const frontend_hostname = window.location.hostname; - if (frontend_hostname !== "localhost") { - endpoint.hostname = frontend_hostname; + endpoint.hostname = frontend_hostname; + if (window.location.protocol === "https:" && endpoint.protocol === "ws:") { + endpoint.protocol = "wss:"; + endpoint.port = ""; // Assume websocket is on https port via load balancer. } } return endpoint