Ver Fonte

maybe ? i'm not sure

Khaleel Al-Adhami há 2 semanas atrás
pai
commit
ce7c597c9a

+ 31 - 3
reflex/.templates/web/utils/state.js

@@ -169,6 +169,21 @@ export const queueEventIfSocketExists = async (events, socket, navigate) => {
   await queueEvents(events, socket, navigate);
 };
 
+/**
+ * Check if a string is a valid HTTP URL.
+ * @param string The string to check.
+ *
+ * @returns The URL object if valid, undefined otherwise.
+ * */
+function urlFrom(string) {
+  try {
+    return new URL(string);
+  } catch {
+    return undefined;
+  }
+  return undefined;
+}
+
 /**
  * Handle frontend event or send the event to the backend via Websocket.
  * @param event The event to send.
@@ -185,10 +200,23 @@ export const applyEvent = async (event, socket, navigate) => {
     }
     if (event.payload.external) {
       window.open(event.payload.path, "_blank", "noopener");
-    } else if (event.payload.replace) {
-      navigate(event.payload.path, { replace: true });
+      return false;
+    }
+    const url = urlFrom(event.payload.path);
+    let pathname = event.payload.path;
+    if (url) {
+      if (url.host !== window.location.host) {
+        // External URL
+        window.location.assign(event.payload.path);
+        return false;
+      } else {
+        pathname = url.pathname + url.search + url.hash;
+      }
+    }
+    if (event.payload.replace) {
+      navigate(pathname, { replace: true });
     } else {
-      navigate(event.payload.path);
+      navigate(pathname);
     }
     return false;
   }

+ 4 - 2
tests/integration/test_deploy_url.py

@@ -82,7 +82,7 @@ def test_deploy_url(deploy_url_sample: AppHarness, driver: WebDriver) -> None:
     assert deploy_url != "http://localhost:3000"
     assert deploy_url == deploy_url_sample.frontend_url
     driver.get(deploy_url)
-    assert driver.current_url == deploy_url + "/"
+    assert driver.current_url.removesuffix("/") == deploy_url.removesuffix("/")
 
 
 def test_deploy_url_in_app(deploy_url_sample: AppHarness, driver: WebDriver) -> None:
@@ -96,5 +96,7 @@ def test_deploy_url_in_app(deploy_url_sample: AppHarness, driver: WebDriver) ->
     driver.find_element(By.ID, "goto_self").click()
 
     WebDriverWait(driver, 10).until(
-        lambda driver: driver.current_url == f"{deploy_url_sample.frontend_url}/"
+        lambda driver: deploy_url_sample.frontend_url
+        and driver.current_url.removesuffix("/")
+        == deploy_url_sample.frontend_url.removesuffix("/")
     )

+ 3 - 1
tests/integration/test_large_state.py

@@ -65,7 +65,9 @@ def test_large_state(var_count: int, tmp_path_factory, benchmark):
         driver = get_driver(large_state)
         try:
             assert large_state.app_instance is not None
-            button = driver.find_element(By.ID, "button")
+            button = large_state.poll_for_result(
+                lambda: driver.find_element(By.ID, "button")
+            )
 
             t = time.time()
             while button.text != "0":