use a default base path for the web router from the config

This commit is contained in:
Evan Almloff 2023-11-14 16:49:06 -06:00
parent 15984b78db
commit 109e10e406
2 changed files with 23 additions and 1 deletions

View file

@ -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"]

View file

@ -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<R>(window: &Window, history: &History) {
@ -165,7 +176,7 @@ impl<R: Routable> WebHistory<R> {
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))
}