浏览代码

Improve the base_path story (#2381)

* Improve the base_path story

* allow base path routes without trailing slash

* remove note about trailing /

* fix clippy empty docs

---------

Co-authored-by: Evan Almloff <evanalmloff@gmail.com>
Drew Pirrone-Brusse 1 年之前
父节点
当前提交
010dcf9533

+ 1 - 1
packages/cli/src/builder.rs

@@ -571,7 +571,7 @@ pub fn gen_page(config: &CrateConfig, manifest: Option<&AssetManifest>, serve: b
     }
     }
 
 
     let base_path = match &config.dioxus_config.web.app.base_path {
     let base_path = match &config.dioxus_config.web.app.base_path {
-        Some(path) => path,
+        Some(path) => path.trim_matches('/'),
         None => ".",
         None => ".",
     };
     };
     let app_name = &config.dioxus_config.application.name;
     let app_name = &config.dioxus_config.application.name;

+ 11 - 6
packages/cli/src/server/web/mod.rs

@@ -116,21 +116,26 @@ async fn start_server(
     router: axum::Router,
     router: axum::Router,
     start_browser: bool,
     start_browser: bool,
     rustls: Option<axum_server::tls_rustls::RustlsConfig>,
     rustls: Option<axum_server::tls_rustls::RustlsConfig>,
-    _config: &CrateConfig,
+    config: &CrateConfig,
 ) -> Result<()> {
 ) -> Result<()> {
     // If plugins, call on_serve_start event
     // If plugins, call on_serve_start event
     #[cfg(feature = "plugin")]
     #[cfg(feature = "plugin")]
-    crate::plugin::PluginManager::on_serve_start(_config)?;
+    crate::plugin::PluginManager::on_serve_start(config)?;
 
 
     // Bind the server to `[::]` and it will LISTEN for both IPv4 and IPv6. (required IPv6 dual stack)
     // Bind the server to `[::]` and it will LISTEN for both IPv4 and IPv6. (required IPv6 dual stack)
     let addr: SocketAddr = format!("0.0.0.0:{}", port).parse().unwrap();
     let addr: SocketAddr = format!("0.0.0.0:{}", port).parse().unwrap();
 
 
     // Open the browser
     // Open the browser
     if start_browser {
     if start_browser {
-        match rustls {
-            Some(_) => _ = open::that(format!("https://localhost:{port}")),
-            None => _ = open::that(format!("http://localhost:{port}")),
-        }
+        let protocol = match rustls {
+            Some(_) => "https",
+            None => "http",
+        };
+        let base_path = match config.dioxus_config.web.app.base_path.as_deref() {
+            Some(base_path) => format!("/{}", base_path.trim_matches('/')),
+            None => "".to_owned(),
+        };
+        _ = open::that(format!("{protocol}://localhost:{port}{base_path}"));
     }
     }
 
 
     let svc = router.into_make_service();
     let svc = router.into_make_service();

+ 6 - 3
packages/cli/src/server/web/server.rs

@@ -62,8 +62,8 @@ pub async fn setup_router(
         .and_then(move |response| async move { Ok(no_cache(file_service_config, response)) })
         .and_then(move |response| async move { Ok(no_cache(file_service_config, response)) })
         .service(ServeDir::new(config.out_dir()));
         .service(ServeDir::new(config.out_dir()));
 
 
-    // Setup websocket
-    let mut router = Router::new().route("/_dioxus/ws", get(ws_handler));
+    // Setup router
+    let mut router = Router::new();
 
 
     // Setup proxy
     // Setup proxy
     for proxy_config in config.dioxus_config.web.proxy {
     for proxy_config in config.dioxus_config.web.proxy {
@@ -83,7 +83,7 @@ pub async fn setup_router(
     router = if let Some(base_path) = config.dioxus_config.web.app.base_path.clone() {
     router = if let Some(base_path) = config.dioxus_config.web.app.base_path.clone() {
         let base_path = format!("/{}", base_path.trim_matches('/'));
         let base_path = format!("/{}", base_path.trim_matches('/'));
         Router::new()
         Router::new()
-            .route(&base_path, axum::routing::any_service(router))
+            .nest(&base_path, router)
             .fallback(get(move || {
             .fallback(get(move || {
                 let base_path = base_path.clone();
                 let base_path = base_path.clone();
                 async move { format!("Outside of the base path: {}", base_path) }
                 async move { format!("Outside of the base path: {}", base_path) }
@@ -92,6 +92,9 @@ pub async fn setup_router(
         router
         router
     };
     };
 
 
+    // Setup websocket
+    router = router.route("/_dioxus/ws", get(ws_handler));
+
     // Setup routes
     // Setup routes
     router = router
     router = router
         .route("/_dioxus/hot_reload", get(hot_reload_handler))
         .route("/_dioxus/hot_reload", get(hot_reload_handler))

+ 1 - 0
packages/desktop/headless_tests/eval.rs

@@ -6,6 +6,7 @@ use serde::Deserialize;
 mod utils;
 mod utils;
 
 
 pub fn main() {
 pub fn main() {
+    #[cfg(not(windows))]
     utils::check_app_exits(app);
     utils::check_app_exits(app);
 }
 }
 
 

+ 1 - 0
packages/interpreter/src/lib.rs

@@ -1,3 +1,4 @@
+#![allow(clippy::empty_docs)]
 #![doc = include_str!("../README.md")]
 #![doc = include_str!("../README.md")]
 #![doc(html_logo_url = "https://avatars.githubusercontent.com/u/79236386")]
 #![doc(html_logo_url = "https://avatars.githubusercontent.com/u/79236386")]
 #![doc(html_favicon_url = "https://avatars.githubusercontent.com/u/79236386")]
 #![doc(html_favicon_url = "https://avatars.githubusercontent.com/u/79236386")]

+ 11 - 11
packages/router/src/history/web.rs

@@ -74,7 +74,9 @@ impl<R: Routable> WebHistory<R> {
         let myself = Self::new_inner(prefix, do_scroll_restoration);
         let myself = Self::new_inner(prefix, do_scroll_restoration);
 
 
         let current_route = myself.current_route();
         let current_route = myself.current_route();
-        let current_url = current_route.to_string();
+        let current_route_str = current_route.to_string();
+        let prefix_str = myself.prefix.as_deref().unwrap_or("");
+        let current_url = format!("{prefix_str}{current_route_str}");
         let state = myself.create_state(current_route);
         let state = myself.create_state(current_route);
         let _ = replace_state_with_url(&myself.history, &state, Some(&current_url));
         let _ = replace_state_with_url(&myself.history, &state, Some(&current_url));
 
 
@@ -130,17 +132,15 @@ where
         let path = location.pathname().unwrap_or_else(|_| "/".into())
         let path = location.pathname().unwrap_or_else(|_| "/".into())
             + &location.search().unwrap_or("".into())
             + &location.search().unwrap_or("".into())
             + &location.hash().unwrap_or("".into());
             + &location.hash().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
-                }
-            }
+        let mut path = match self.prefix {
+            None => &path,
+            Some(ref prefix) => path.strip_prefix(prefix).unwrap_or(prefix),
         };
         };
-        R::from_str(&path).unwrap_or_else(|err| panic!("{}", err))
+        // If the path is empty, parse the root route instead
+        if path.is_empty() {
+            path = "/"
+        }
+        R::from_str(path).unwrap_or_else(|err| panic!("{}", err))
     }
     }
 
 
     fn full_path(&self, state: &R) -> String {
     fn full_path(&self, state: &R) -> String {