From 109e10e406e6bf1c90f4c635e79126a43d9e573f Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Tue, 14 Nov 2023 16:49:06 -0600 Subject: [PATCH] use a default base path for the web router from the config --- packages/router/Cargo.toml | 1 + packages/router/src/history/web.rs | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/router/Cargo.toml b/packages/router/Cargo.toml index da9d3fe85..f07c3a724 100644 --- a/packages/router/Cargo.toml +++ b/packages/router/Cargo.toml @@ -30,6 +30,7 @@ gloo-utils = { version = "0.1.6", optional = true } dioxus-liveview = { workspace = true, optional = true } dioxus-ssr = { workspace = true, optional = true } tokio = { workspace = true, features = ["full"], optional = true } +dioxus-cli-config.workspace = true [features] default = ["web"] diff --git a/packages/router/src/history/web.rs b/packages/router/src/history/web.rs index 789eed76e..5b6ae0528 100644 --- a/packages/router/src/history/web.rs +++ b/packages/router/src/history/web.rs @@ -13,6 +13,17 @@ use super::{ HistoryProvider, }; +#[allow(dead_code)] +fn base_path() -> Option<&'static str> { + dioxus_cli_config::CURRENT_CONFIG.as_ref().ok().and_then(|c| { + c.dioxus_config + .web + .app + .base_path + .as_deref() + }) + } + #[cfg(not(feature = "serde"))] #[allow(clippy::extra_unused_type_parameters)] fn update_scroll(window: &Window, history: &History) { @@ -165,7 +176,7 @@ impl WebHistory { history, listener_navigation: None, listener_animation_frame: Default::default(), - prefix, + prefix: prefix.or_else(||base_path().map(|s| s.to_string())), window, phantom: Default::default(), } @@ -198,6 +209,16 @@ where let location = self.window.location(); let path = location.pathname().unwrap_or_else(|_| "/".into()) + &location.search().unwrap_or("".into()); + let path = match self.prefix { + None => path, + Some(ref prefix) => { + if path.starts_with(prefix) { + path[prefix.len()..].to_string() + } else { + path + } + } + }; R::from_str(&path).unwrap_or_else(|err| panic!("{}", err)) }