From a35d4d557d81ca12b3962eade5820a624bd7c863 Mon Sep 17 00:00:00 2001 From: ryan Date: Thu, 20 Feb 2025 15:38:43 +0300 Subject: [PATCH] feat(ssr): added get_current_url function for SSR support --- src/components/items_list.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/components/items_list.rs b/src/components/items_list.rs index dd3ccdf..70a19e3 100644 --- a/src/components/items_list.rs +++ b/src/components/items_list.rs @@ -84,8 +84,19 @@ pub fn ItemsList( // Signal to store the fetched property labels let (property_labels, set_property_labels) = create_signal(HashMap::::new()); + #[cfg(feature = "ssr")] fn get_current_url() -> String { - window() + use leptos::use_context; + use actix_web::HttpRequest; + + use_context::() + .map(|req| req.uri().to_string()) + .unwrap_or_default() + } + + #[cfg(not(feature = "ssr"))] + fn get_current_url() -> String { + web_sys::window() .and_then(|win| win.location().href().ok()) .unwrap_or_else(|| "".to_string()) } @@ -190,10 +201,10 @@ pub fn ItemsList( let item_to_send = ItemToSend { url: current_url.to_string(), - id: item.id, - name: item.name, - description: item.description, - wikidata_id: item.wikidata_id, + id: item.id, + name: item.name, + description: item.description, + wikidata_id: item.wikidata_id, custom_properties, // Use the serialized string };