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>
This commit is contained in:
Drew Pirrone-Brusse 2024-05-03 14:38:07 -04:00 committed by GitHub
parent 472031d1f5
commit 010dcf9533
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 31 additions and 21 deletions

View file

@ -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;

View file

@ -116,21 +116,26 @@ async fn start_server(
router: axum::Router,
start_browser: bool,
rustls: Option<axum_server::tls_rustls::RustlsConfig>,
_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();

View file

@ -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))

View file

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

View file

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

View file

@ -74,7 +74,9 @@ impl<R: Routable> WebHistory<R> {
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(&current_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 {