Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix router #1856

Merged
merged 1 commit into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/yew-router/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ where
fn push_impl(url: String) {
let history = yew::utils::window().history().expect("no history");

let path = build_path_with_base(&url);

history
.push_state_with_url(&JsValue::NULL, "", Some(&build_path_with_base(&url)))
.push_state_with_url(&JsValue::NULL, "", Some(&path))
.expect("push history");
let event = Event::new("popstate").unwrap();
yew::utils::window()
Expand Down
30 changes: 16 additions & 14 deletions packages/yew-router/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
use wasm_bindgen::JsCast;

fn strip_slash(path: String) -> String {
if path != "/" {
path.strip_suffix("/")
.map(|it| it.to_string())
.unwrap_or(path)
} else {
path
}
}

pub fn base_url() -> Option<String> {
match yew::utils::document().query_selector("base[href]") {
Ok(Some(base)) => {
Expand All @@ -18,15 +8,27 @@ pub fn base_url() -> Option<String> {
let url = web_sys::Url::new(&base).unwrap();
let base = url.pathname();

let base = strip_slash(base);
let base = if base != "/" {
base.strip_suffix("/")
.map(|it| it.to_string())
.unwrap_or(base)
} else {
return None;
};

Some(base)
}
_ => None,
}
}

pub fn build_path_with_base(to: &str) -> String {
let to = format!("{}{}", base_url().as_deref().unwrap_or(""), to);

strip_slash(to)
format!(
"{}{}",
base_url()
.as_deref()
.map(|it| it.strip_suffix("/").unwrap_or(it))
.unwrap_or(""),
to
)
}
23 changes: 23 additions & 0 deletions packages/yew-router/tests/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ enum Routes {

#[test]
fn test_base_url() {
document().head().unwrap().set_inner_html(r#""#);

assert_eq!(base_url(), None);

document()
Expand Down Expand Up @@ -64,3 +66,24 @@ fn test_get_query_params() {
map
});
}

#[test]
fn test_build_base_url() {
document().head().unwrap().set_inner_html(r#""#);

assert_eq!(build_path_with_base("/posts"), "/posts");

document()
.head()
.unwrap()
.set_inner_html(r#"<base href="/router">"#);

assert_eq!(build_path_with_base("/posts/"), "/router/posts/");

document()
.head()
.unwrap()
.set_inner_html(r#"<base href="/">"#);

assert_eq!(build_path_with_base("/posts"), "/posts");
}
3 changes: 1 addition & 2 deletions packages/yew-router/tests/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ fn component() -> Html {
// - 404 redirects
#[test]
fn router_works() {
let app = App::<Comp>::new();
app.mount(yew::utils::document().get_element_by_id("output").unwrap());
yew::start_app_in_element::<Comp>(yew::utils::document().get_element_by_id("output").unwrap());

assert_eq!("Home", obtain_result_by_id("result"));

Expand Down