From 0ce326566e2242163420f52828a01305e3b70a70 Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Wed, 9 Mar 2022 16:26:13 -0500 Subject: [PATCH] wip: fix router slashing --- packages/router/src/components/route.rs | 1 + packages/router/src/service.rs | 23 ++++++++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/packages/router/src/components/route.rs b/packages/router/src/components/route.rs index 530ad77a0..9adf67c35 100644 --- a/packages/router/src/components/route.rs +++ b/packages/router/src/components/route.rs @@ -54,6 +54,7 @@ pub fn Route<'a>(cx: Scope<'a, RouteProps<'a>>) -> Element { }); log::debug!("Checking Route: {:?}", cx.props.to); + if router_root.should_render(cx.scope_id()) { log::debug!("Route should render: {:?}", cx.scope_id()); cx.render(rsx!(&cx.props.children)) diff --git a/packages/router/src/service.rs b/packages/router/src/service.rs index 5cf93c597..2e8f17417 100644 --- a/packages/router/src/service.rs +++ b/packages/router/src/service.rs @@ -177,12 +177,10 @@ impl RouterCore { let roots = self.slots.borrow(); if let Some(route) = roots.get(&scope) { - if route_matches_path( - &self.current_location().url, - route, - self.cfg.base_url.as_ref(), - ) || route.is_empty() - { + let cur = &self.current_location().url; + log::debug!("Checking if {} matches {}", cur, route); + + if route_matches_path(cur, route, self.cfg.base_url.as_ref()) || route.is_empty() { self.route_found.set(Some(scope)); true } else { @@ -217,18 +215,25 @@ fn clean_path(path: &str) -> &str { fn route_matches_path(cur: &Url, attempt: &str, base_url: Option<&String>) -> bool { let cur_piece_iter = cur.path_segments().unwrap(); - let cur_pieces = match base_url { + let mut cur_pieces = match base_url { // baseurl is naive right now and doesn't support multiple nesting levels Some(_) => cur_piece_iter.skip(1).collect::>(), None => cur_piece_iter.collect::>(), }; - let attempt_pieces = clean_path(attempt).split('/').collect::>(); - if attempt == "/" && cur_pieces.len() == 1 && cur_pieces[0].is_empty() { return true; } + // allow slashes at the end of the path + if cur_pieces.last() == Some(&"") { + cur_pieces.pop(); + } + + let attempt_pieces = clean_path(attempt).split('/').collect::>(); + + log::debug!("Comparing {:?} to {:?}", cur_pieces, attempt_pieces); + if attempt_pieces.len() != cur_pieces.len() { return false; }