diff --git a/packages/yew-router/src/router.rs b/packages/yew-router/src/router.rs index 461317e02bc..f0a1a61fa38 100644 --- a/packages/yew-router/src/router.rs +++ b/packages/yew-router/src/router.rs @@ -2,7 +2,6 @@ use std::borrow::Cow; use std::rc::Rc; -use gloo::console::console_dbg; use yew::prelude::*; use yew::virtual_dom::AttrValue; @@ -78,35 +77,25 @@ fn base_router(props: &RouterProps) -> Html { let basename = basename.map(|m| strip_slash_suffix(&m).to_string()); let navigator = Navigator::new(history.clone(), basename.clone()); - { - let history = history.clone(); - let navigator = navigator.clone(); - let basename = basename.clone(); - let old_basename = use_state_eq(|| Option::::None); - console_dbg!("render"); - // Can't use `use_Effect_with` since need to track old and new, not just new. - use_effect(move || { - console_dbg!(format!("{basename:?} {:?}", *old_basename)); - if basename != *old_basename { - // If `old_basename` is `Some`, path is probably prefixed with `old_basename`. - // If `old_basename` is `None`, path may or may not be prefixed with the new `basename`, - // depending on whether this is the first render. - let old_navigator: Navigator = Navigator::new( - history.clone(), - old_basename.as_ref().or(basename.as_ref()).cloned(), - ); - old_basename.set(basename.clone()); - let location = history.location(); - let stripped = old_navigator.strip_basename(Cow::from(location.path())); - let prefixed = navigator.prefix_basename(&stripped); - console_dbg!(format!("s {stripped} {prefixed}")); - if false && prefixed != location.path() { - history - .replace_with_query(prefixed, Raw(location.query_str())) - .unwrap_or_else(|never| match never {}) - } - } - }); + let old_basename = use_state_eq(|| Option::::None); + if basename != *old_basename { + // If `old_basename` is `Some`, path is probably prefixed with `old_basename`. + // If `old_basename` is `None`, path may or may not be prefixed with the new `basename`, + // depending on whether this is the first render. + let old_navigator = Navigator::new( + history.clone(), + old_basename.as_ref().or(basename.as_ref()).cloned(), + ); + old_basename.set(basename.clone()); + let location = history.location(); + let stripped = old_navigator.strip_basename(Cow::from(location.path())); + let prefixed = navigator.prefix_basename(&stripped); + + if prefixed != location.path() { + history + .replace_with_query(prefixed, Raw(location.query_str())) + .unwrap_or_else(|never| match never {}); + } } let navi_ctx = NavigatorContext { navigator }; diff --git a/packages/yew-router/tests/link.rs b/packages/yew-router/tests/link.rs index 057263ccf32..47552751ef3 100644 --- a/packages/yew-router/tests/link.rs +++ b/packages/yew-router/tests/link.rs @@ -1,6 +1,5 @@ use std::time::Duration; -use gloo::console::console_dbg; use serde::{Deserialize, Serialize}; use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure}; use yew::functional::function_component; @@ -94,7 +93,7 @@ async fn link_in_browser_router() { let div = gloo::utils::document().create_element("div").unwrap(); let _ = div.set_attribute("id", "browser-router"); let _ = gloo::utils::body().append_child(&div); - yew::Renderer::::with_root(div).render(); + let handle = yew::Renderer::::with_root(div).render(); sleep(Duration::ZERO).await; @@ -113,6 +112,8 @@ async fn link_in_browser_router() { "/search?q=Rust&lang=en_US", link_href("#browser-router ul > li.search-q-lang > a") ); + + handle.destroy(); } #[derive(PartialEq, Properties)] @@ -122,7 +123,6 @@ struct BasenameProps { #[function_component(RootForBasename)] fn root_for_basename(props: &BasenameProps) -> Html { - console_dbg!("outer render"); html! { @@ -142,16 +142,12 @@ async fn link_with_basename() { ) .render(); - for _ in 0..10 { - sleep(Duration::from_millis(100)).await; - } + sleep(Duration::ZERO).await; - /* assert_eq!( "/base/", gloo::utils::window().location().pathname().unwrap() ); - */ assert_eq!("/base/posts", link_href("#with-basename ul > li.posts > a")); assert_eq!( @@ -177,18 +173,12 @@ async fn link_with_basename() { basename: Some("/bayes/".to_owned()), }); - for _ in 0..10 { - sleep(Duration::from_millis(100)).await; - } - - console_dbg!("before assert"); + sleep(Duration::ZERO).await; - /* assert_eq!( "/bayes/", gloo::utils::window().location().pathname().unwrap() ); - */ assert_eq!( "/bayes/posts", @@ -198,9 +188,7 @@ async fn link_with_basename() { // Some -> None handle.update(BasenameProps { basename: None }); - for _ in 0..10 { - sleep(Duration::from_millis(100)).await; - } + sleep(Duration::ZERO).await; assert_eq!("/", gloo::utils::window().location().pathname().unwrap()); @@ -208,12 +196,10 @@ async fn link_with_basename() { // None -> Some handle.update(BasenameProps { - basename: Some("bass".to_string()), + basename: Some("/bass/".to_string()), }); - for _ in 0..10 { - sleep(Duration::from_millis(100)).await; - } + sleep(Duration::ZERO).await; assert_eq!( "/bass/", @@ -221,6 +207,8 @@ async fn link_with_basename() { ); assert_eq!("/bass/posts", link_href("#with-basename ul > li.posts > a")); + + handle.destroy(); } #[function_component(RootForHashRouter)] @@ -236,7 +224,7 @@ async fn link_in_hash_router() { let div = gloo::utils::document().create_element("div").unwrap(); let _ = div.set_attribute("id", "hash-router"); let _ = gloo::utils::body().append_child(&div); - yew::Renderer::::with_root(div).render(); + let handle = yew::Renderer::::with_root(div).render(); sleep(Duration::ZERO).await; @@ -255,6 +243,8 @@ async fn link_in_hash_router() { "#/search?q=Rust&lang=en_US", link_href("#hash-router ul > li.search-q-lang > a") ); + + handle.destroy(); } // These cannot be run in concurrently because they all read/write the URL.