Browse Source

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

Evan Almloff 1 year ago
parent
commit
109e10e406
2 changed files with 23 additions and 1 deletions
  1. 1 0
      packages/router/Cargo.toml
  2. 22 1
      packages/router/src/history/web.rs

+ 1 - 0
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"]

+ 22 - 1
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<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))
     }