From 155a2467bedb5204462fb2d8be487233e483fa02 Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Tue, 28 Jan 2025 10:09:27 -0800 Subject: [PATCH] fix: don't emit hash fragment for internal links if hash is empty (#3660) * fix: don't emit hash fragment for internal links if hash is empty * add test --- packages/router-macro/src/hash.rs | 7 ++++++- packages/router/tests/via_ssr/link.rs | 28 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/router-macro/src/hash.rs b/packages/router-macro/src/hash.rs index 2a16bc4e02..c199a0ac25 100644 --- a/packages/router-macro/src/hash.rs +++ b/packages/router-macro/src/hash.rs @@ -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)?; + } + } } } diff --git a/packages/router/tests/via_ssr/link.rs b/packages/router/tests/via_ssr/link.rs index efc8201f66..2075d632ac 100644 --- a/packages/router/tests/via_ssr/link.rs +++ b/packages/router/tests/via_ssr/link.rs @@ -431,3 +431,31 @@ fn with_child_route() { "

App

Parent LinkChild Link 1Child Link 2" ); } + +#[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::("/#test"), + "

App

LinkEmpty" + ); +}