Browse Source

allow base_path in the CLI

Evan Almloff 1 year ago
parent
commit
cd48b3b7f9

+ 1 - 1
packages/cli/Cargo.toml

@@ -37,7 +37,7 @@ hyper-rustls = "0.23.2"
 indicatif = "0.17.5"
 subprocess = "0.2.9"
 
-axum = { version = "0.5.1", features = ["ws", "headers"] }
+axum = { version = "0.6.20", features = ["ws", "headers"] }
 axum-server = { version = "0.5.1", features = ["tls-rustls"] }
 tower-http = { version = "0.2.2", features = ["full"] }
 headers = "0.3.7"

+ 0 - 3
packages/cli/src/cli/serve.rs

@@ -33,9 +33,6 @@ impl Serve {
             crate_config.set_features(self.serve.features.unwrap());
         }
 
-        // Subdirectories don't work with the server
-        crate_config.dioxus_config.web.app.base_path = None;
-
         let platform = self
             .serve
             .platform

+ 15 - 0
packages/cli/src/server/web/mod.rs

@@ -310,6 +310,21 @@ async fn setup_router(
         },
     ));
 
+    router = if let Some(base_path) = config.dioxus_config.web.app.base_path.clone() {
+        let base_path = format!("/{}", base_path.trim_matches('/'));
+        Router::new()
+            .nest_service(
+                &base_path,
+                axum::routing::method_routing::any_service(router),
+            )
+            .fallback(get(move || {
+                let base_path = base_path.clone();
+                async move { format!("Outside of the base path: {}", base_path) }
+            }))
+    } else {
+        router
+    };
+
     // Setup routes
     router = router
         .route("/_dioxus/hot_reload", get(hot_reload_handler))

+ 12 - 9
packages/router/src/history/web.rs

@@ -15,14 +15,13 @@ use super::{
 
 #[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()
-      })
-  }
+    let base_path = dioxus_cli_config::CURRENT_CONFIG
+        .as_ref()
+        .ok()
+        .and_then(|c| c.dioxus_config.web.app.base_path.as_deref());
+    tracing::trace!("Using base_path from Dioxus.toml: {:?}", base_path);
+    base_path
+}
 
 #[cfg(not(feature = "serde"))]
 #[allow(clippy::extra_unused_type_parameters)]
@@ -171,12 +170,16 @@ impl<R: Routable> WebHistory<R> {
                 .expect("`history` can set scroll restoration");
         }
 
+        let prefix = prefix
+            .or_else(|| base_path().map(|s| s.to_string()))
+            .map(|prefix| format!("/{}", prefix.trim_matches('/')));
+
         Self {
             do_scroll_restoration,
             history,
             listener_navigation: None,
             listener_animation_frame: Default::default(),
-            prefix: prefix.or_else(||base_path().map(|s| s.to_string())),
+            prefix,
             window,
             phantom: Default::default(),
         }