Просмотр исходного кода

Assume wss:// with no port when frontend is HTTPS (#2129)

Masen Furer 1 год назад
Родитель
Сommit
53d4c438ed
1 измененных файлов с 9 добавлено и 5 удалено
  1. 9 5
      reflex/.templates/web/utils/state.js

+ 9 - 5
reflex/.templates/web/utils/state.js

@@ -12,6 +12,9 @@ import { initialEvents } from "utils/context.js"
 const EVENTURL = env.EVENT
 const EVENTURL = env.EVENT
 const UPLOADURL = env.UPLOAD
 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.
 // Global variable to hold the token.
 let token;
 let token;
 
 
@@ -74,12 +77,13 @@ export const getToken = () => {
 export const getEventURL = () => {
 export const getEventURL = () => {
   // Get backend URL object from the endpoint.
   // Get backend URL object from the endpoint.
   const endpoint = new URL(EVENTURL);
   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;
     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
   return endpoint