Fix the base path in desktop builds (#3949)

* Fix the base path in desktop builds

---------

Co-authored-by: Jonathan Kelley <jkelleyrtp@gmail.com>
This commit is contained in:
Evan AlmloffandJonathan Kelley authored and GitHub committed 2025-05-07 13:52:56 -07:00
1 parent 0c0f0e96df
commit be01cf1485
4 files changed
+638 -545

No files matched your search

+2 -2
View File
@@ -448,8 +448,8 @@ impl AppBuilder {
("RUST_BACKTRACE", "1".to_string()),
];
if let Some(base_path) = &krate.config.web.app.base_path {
envs.push((dioxus_cli_config::ASSET_ROOT_ENV, base_path.clone()));
if let Some(base_path) = krate.base_path() {
envs.push((dioxus_cli_config::ASSET_ROOT_ENV, base_path.to_string()));
}
if let Some(env_filter) = env::var_os("RUST_LOG").and_then(|e| e.into_string().ok()) {
+24 -4
View File
@@ -2014,8 +2014,8 @@ session_cache_dir: {}"#,
// If this is a release build, bake the base path and title into the binary with env vars.
// todo: should we even be doing this? might be better being a build.rs or something else.
if self.release {
if let Some(base_path) = &self.config.web.app.base_path {
env_vars.push((ASSET_ROOT_ENV, base_path.clone()));
if let Some(base_path) = self.base_path() {
env_vars.push((ASSET_ROOT_ENV, base_path.to_string()));
}
env_vars.push((APP_TITLE_ENV, self.config.web.app.title.clone()));
}
@@ -3612,7 +3612,7 @@ session_cache_dir: {}"#,
// Add the base path to the head if this is a debug build
if self.is_dev_build() {
if let Some(base_path) = &self.config.web.app.base_path {
if let Some(base_path) = &self.base_path() {
head_resources.push_str(&format_base_path_meta_element(base_path));
}
}
@@ -3698,7 +3698,7 @@ r#" <script>
/// Replace any special placeholders in the HTML with resolved values
fn replace_template_placeholders(&self, html: &mut String, wasm_path: &str, js_path: &str) {
let base_path = self.config.web.app.base_path();
let base_path = self.base_path_or_default();
*html = html.replace("{base_path}", base_path);
let app_name = &self.executable_name();
@@ -3729,4 +3729,24 @@ r#" <script>
content.insert_str(pos, with);
}
}
/// Get the base path from the config or None if this is not a web or server build
pub(crate) fn base_path(&self) -> Option<&str> {
self.config
.web
.app
.base_path
.as_deref()
.filter(|_| matches!(self.platform, Platform::Web | Platform::Server))
}
/// Get the normalized base path for the application with `/` trimmed from both ends. If the base path is not set, this will return `.`.
pub(crate) fn base_path_or_default(&self) -> &str {
let trimmed_path = self.base_path().unwrap_or_default().trim_matches('/');
if trimmed_path.is_empty() {
"."
} else {
trimmed_path
}
}
}
-16
View File
@@ -98,22 +98,6 @@ pub(crate) struct WebAppConfig {
pub(crate) base_path: Option<String>,
}
impl WebAppConfig {
/// Get the normalized base path for the application with `/` trimmed from both ends. If the base path is not set, this will return `.`.
pub(crate) fn base_path(&self) -> &str {
let trimmed_path = self
.base_path
.as_deref()
.unwrap_or_default()
.trim_matches('/');
if trimmed_path.is_empty() {
"."
} else {
trimmed_path
}
}
}
impl Default for WebAppConfig {
fn default() -> Self {
Self {