Skip to content

Commit

Permalink
fix: don't emit hash fragment for internal links if hash is empty (#3660
Browse files Browse the repository at this point in the history
)

* fix: don't emit hash fragment for internal links if hash is empty

* add test
  • Loading branch information
jkelleyrtp authored Jan 28, 2025
1 parent 861e6c1 commit 155a246
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/router-macro/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ impl HashFragment {
pub fn write(&self) -> TokenStream2 {
let ident = &self.ident;
quote! {
write!(f, "#{}", #ident)?;
{
let __hash = #ident.to_string();
if !__hash.is_empty() {
write!(f, "#{}", __hash)?;
}
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions packages/router/tests/via_ssr/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,3 +431,31 @@ fn with_child_route() {
"<h1>App</h1><a href=\"/test\">Parent Link</a><a href=\"/child/this-is-a-child-route\">Child Link 1</a><a href=\"/child/this-is-a-child-route\">Child Link 2</a>"
);
}

#[test]
fn with_hash_segment() {
#[derive(Routable, Clone)]
enum Route {
#[route("/#:data")]
Root { data: String },
}

#[component]
fn Root(data: String) -> Element {
rsx! {
Link {
to: Route::Root { data: "test".to_string() },
"Link"
}
Link {
to: Route::Root { data: "".to_string() },
"Empty"
}
}
}

assert_eq!(
prepare_at::<Route>("/#test"),
"<h1>App</h1><a href=\"/#test\" aria-current=\"page\">Link</a><a href=\"/\">Empty</a>"
);
}

0 comments on commit 155a246

Please sign in to comment.