From 47a6901695a84b32a879425e96a4241b813a112e Mon Sep 17 00:00:00 2001 From: Masen Furer Date: Mon, 12 Feb 2024 15:04:59 -0800 Subject: [PATCH] banner.py: fix import specification for getBackendURL (#2584) * banner.py: fix import specification for getBackendURL Use the constant Dirs.STATE_PATH * state.js: only `getBackendURL` dynamically when running client side During server side rendering, `getBackendURL` cannot access the current location from the `window`, because there is no `window`. * Better client-side context checking Thanks jackie --- reflex/.templates/web/utils/state.js | 4 ++-- reflex/components/core/banner.py | 4 ++-- reflex/components/core/banner.pyi | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/reflex/.templates/web/utils/state.js b/reflex/.templates/web/utils/state.js index d47c5da0c..eda4f1cf5 100644 --- a/reflex/.templates/web/utils/state.js +++ b/reflex/.templates/web/utils/state.js @@ -64,7 +64,7 @@ export const getToken = () => { if (token) { return token; } - if (window) { + if (typeof window !== 'undefined') { if (!window.sessionStorage.getItem(TOKEN_KEY)) { window.sessionStorage.setItem(TOKEN_KEY, generateUUID()); } @@ -81,7 +81,7 @@ export const getToken = () => { export const getBackendURL = (url_str) => { // Get backend URL object from the endpoint. const endpoint = new URL(url_str); - if (SAME_DOMAIN_HOSTNAMES.includes(endpoint.hostname)) { + if ((typeof window !== 'undefined') && SAME_DOMAIN_HOSTNAMES.includes(endpoint.hostname)) { // Use the frontend domain to access the backend const frontend_hostname = window.location.hostname; endpoint.hostname = frontend_hostname; diff --git a/reflex/components/core/banner.py b/reflex/components/core/banner.py index 54164297f..c0e13820e 100644 --- a/reflex/components/core/banner.py +++ b/reflex/components/core/banner.py @@ -9,7 +9,7 @@ from reflex.components.chakra.overlay.modal import Modal from reflex.components.chakra.typography import Text from reflex.components.component import Component from reflex.components.core.cond import cond -from reflex.constants import Hooks, Imports +from reflex.constants import Dirs, Hooks, Imports from reflex.utils import imports from reflex.vars import Var, VarData @@ -34,7 +34,7 @@ class WebsocketTargetURL(Bare): def _get_imports(self) -> imports.ImportDict: return { - "/utils/state.js": [imports.ImportVar(tag="getBackendURL")], + f"/{Dirs.STATE_PATH}": [imports.ImportVar(tag="getBackendURL")], "/env.json": [imports.ImportVar(tag="env", is_default=True)], } diff --git a/reflex/components/core/banner.pyi b/reflex/components/core/banner.pyi index 69fe11b58..00f9a5121 100644 --- a/reflex/components/core/banner.pyi +++ b/reflex/components/core/banner.pyi @@ -14,7 +14,7 @@ from reflex.components.chakra.overlay.modal import Modal from reflex.components.chakra.typography import Text from reflex.components.component import Component from reflex.components.core.cond import cond -from reflex.constants import Hooks, Imports +from reflex.constants import Dirs, Hooks, Imports from reflex.utils import imports from reflex.vars import Var, VarData