From 010dcf9533ee7f5bc3cc340d80354ebb5d1e2d4c Mon Sep 17 00:00:00 2001 From: Drew Pirrone-Brusse Date: Fri, 3 May 2024 14:38:07 -0400 Subject: [PATCH] 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 --- packages/cli/src/builder.rs | 2 +- packages/cli/src/server/web/mod.rs | 17 +++++++++++------ packages/cli/src/server/web/server.rs | 9 ++++++--- packages/desktop/headless_tests/eval.rs | 1 + packages/interpreter/src/lib.rs | 1 + packages/router/src/history/web.rs | 22 +++++++++++----------- 6 files changed, 31 insertions(+), 21 deletions(-) diff --git a/packages/cli/src/builder.rs b/packages/cli/src/builder.rs index 4d9c638e0..f7fed41e7 100644 --- a/packages/cli/src/builder.rs +++ b/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 { - Some(path) => path, + Some(path) => path.trim_matches('/'), None => ".", }; let app_name = &config.dioxus_config.application.name; diff --git a/packages/cli/src/server/web/mod.rs b/packages/cli/src/server/web/mod.rs index b366dabeb..e482e7406 100644 --- a/packages/cli/src/server/web/mod.rs +++ b/packages/cli/src/server/web/mod.rs @@ -116,21 +116,26 @@ async fn start_server( router: axum::Router, start_browser: bool, rustls: Option, - _config: &CrateConfig, + config: &CrateConfig, ) -> Result<()> { // If plugins, call on_serve_start event #[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) let addr: SocketAddr = format!("0.0.0.0:{}", port).parse().unwrap(); // Open the 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(); diff --git a/packages/cli/src/server/web/server.rs b/packages/cli/src/server/web/server.rs index ef1c4b804..eb9dc208a 100644 --- a/packages/cli/src/server/web/server.rs +++ b/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)) }) .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 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() { let base_path = format!("/{}", base_path.trim_matches('/')); Router::new() - .route(&base_path, axum::routing::any_service(router)) + .nest(&base_path, router) .fallback(get(move || { let base_path = base_path.clone(); async move { format!("Outside of the base path: {}", base_path) } @@ -92,6 +92,9 @@ pub async fn setup_router( router }; + // Setup websocket + router = router.route("/_dioxus/ws", get(ws_handler)); + // Setup routes router = router .route("/_dioxus/hot_reload", get(hot_reload_handler)) diff --git a/packages/desktop/headless_tests/eval.rs b/packages/desktop/headless_tests/eval.rs index 3d61dba76..4936fb063 100644 --- a/packages/desktop/headless_tests/eval.rs +++ b/packages/desktop/headless_tests/eval.rs @@ -6,6 +6,7 @@ use serde::Deserialize; mod utils; pub fn main() { + #[cfg(not(windows))] utils::check_app_exits(app); } diff --git a/packages/interpreter/src/lib.rs b/packages/interpreter/src/lib.rs index 45b85eec5..9b1b3b1f6 100644 --- a/packages/interpreter/src/lib.rs +++ b/packages/interpreter/src/lib.rs @@ -1,3 +1,4 @@ +#![allow(clippy::empty_docs)] #![doc = include_str!("../README.md")] #![doc(html_logo_url = "https://avatars.githubusercontent.com/u/79236386")] #![doc(html_favicon_url = "https://avatars.githubusercontent.com/u/79236386")] diff --git a/packages/router/src/history/web.rs b/packages/router/src/history/web.rs index 57e577b3b..c1925da28 100644 --- a/packages/router/src/history/web.rs +++ b/packages/router/src/history/web.rs @@ -74,7 +74,9 @@ impl WebHistory { let myself = Self::new_inner(prefix, do_scroll_restoration); 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 _ = replace_state_with_url(&myself.history, &state, Some(¤t_url)); @@ -130,17 +132,15 @@ where let path = location.pathname().unwrap_or_else(|_| "/".into()) + &location.search().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 {