Fix base path on web and fullstack (#3247)

* Create a crate for constant serialization of config structs for manganis

* use SerializeConst for the image asset builder

* switch to a declarative macro for assets

* clean up asset macro a bit

* add serializable options for each asset type

* serialize const vec

* Add unique path formatting

* implement the new manganis macro

* optimize assets in the CLI

* Fix clippy

* Fix assets with dioxus formattable

* reduce fuzzing test limits

* use the new syntax in examples

* fix formatting

* Final doc and test pass on const-serialize

* fix avif support

* Fix manganis doc tests

* cache asset optimization

* Split out asset and bundled asset

* make hash pointer width independent

* remove const serialize cargo lock

* Fix manganis macro docs

* all tests passing

* add constvec::is_empty method to fix clippy lint

* remove nasm feature

* simplify test_rsplit_once test so typos passes

* fix range syntax for stable

* revert example change from clippy fix

* remove dioxus-static-site-generation workspace dependency

* always accept unix paths

* fix windows path seperator

* fix folder asset hash

* Optimize assets in a blocking task

* Fix asset options docs

* Document Asset and BundledAsset

* move manganis back into it's own folder

* simplify the linker macro a bit

* add more docs to AssetParser expansion

* fix manganis core doc test

* add image format helpers

* Fill in new cargo.tomls

* fix folders with explicit options

* Split by  both unix and windows path separators and take the smallest one

* fix string length

* Fix base path on web and fullstack

* Fix assets with base path

* fix fullstack base route with trailing slash

* fix clippy
This commit is contained in:
Evan Almloff authored and GitHub committed 2024-11-27 10:40:55 +03:00
1 parent 1e8693a0e8
commit 3d62fbf021
20 files changed
+254 -41

No files matched your search

+10
View File
@@ -5,6 +5,7 @@ use crate::{assets::AssetManifest, TraceSrc};
use crate::{link::LinkAction, BuildArgs};
use crate::{AppBundle, Platform};
use anyhow::Context;
use dioxus_cli_config::{APP_TITLE_ENV, ASSET_ROOT_ENV};
use serde::Deserialize;
use std::{
path::{Path, PathBuf},
@@ -540,6 +541,15 @@ impl BuildRequest {
// env_vars.push(("PATH", extended_path));
};
// If this is a release build, bake the base path and title
// into the binary with env vars
if self.build.release {
if let Some(base_path) = &self.krate.config.web.app.base_path {
env_vars.push((ASSET_ROOT_ENV, base_path.clone()));
}
env_vars.push((APP_TITLE_ENV, self.krate.config.web.app.title.clone()));
}
Ok(env_vars)
}
+9
View File
@@ -1,3 +1,5 @@
use dioxus_cli_config::format_base_path_meta_element;
use crate::error::Result;
use crate::BuildRequest;
use std::fmt::Write;
@@ -66,6 +68,13 @@ impl BuildRequest {
)?;
}
// Add the base path to the head if this is a debug build
if self.is_dev_build() {
if let Some(base_path) = &self.krate.config.web.app.base_path {
head_resources.push_str(&format_base_path_meta_element(base_path));
}
}
if !style_list.is_empty() {
self.send_resource_deprecation_warning(style_list, ResourceType::Style);
}
+9 -3
View File
@@ -101,9 +101,15 @@ pub(crate) struct WebAppConfig {
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 {
match &self.base_path {
Some(path) => path.trim_matches('/'),
None => ".",
let trimmed_path = self
.base_path
.as_deref()
.unwrap_or_default()
.trim_matches('/');
if trimmed_path.is_empty() {
"."
} else {
trimmed_path
}
}
}
+11 -3
View File
@@ -69,7 +69,11 @@ impl AppHandle {
// Set the env vars that the clients will expect
// These need to be stable within a release version (ie 0.6.0)
let mut envs = vec![
("DIOXUS_CLI_ENABLED", "true".to_string()),
(dioxus_cli_config::CLI_ENABLED_ENV, "true".to_string()),
(
dioxus_cli_config::APP_TITLE_ENV,
self.app.build.krate.config.web.app.title.clone(),
),
("RUST_BACKTRACE", "1".to_string()),
(
dioxus_cli_config::DEVSERVER_RAW_ADDR_ENV,
@@ -80,6 +84,10 @@ impl AppHandle {
("CARGO_MANIFEST_DIR", "".to_string()),
];
if let Some(base_path) = &self.app.build.krate.config.web.app.base_path {
envs.push((dioxus_cli_config::ASSET_ROOT_ENV, base_path.clone()));
}
if let Some(addr) = fullstack_address {
envs.push((dioxus_cli_config::SERVER_IP_ENV, addr.ip().to_string()));
envs.push((dioxus_cli_config::SERVER_PORT_ENV, addr.port().to_string()));
@@ -109,7 +117,7 @@ impl AppHandle {
Platform::Web => {
// Only the first build we open the web app, after that the user knows it's running
if open_browser {
self.open_web(envs, devserver_ip);
self.open_web(devserver_ip);
}
None
@@ -212,7 +220,7 @@ impl AppHandle {
/// Open the web app by opening the browser to the given address.
/// Check if we need to use https or not, and if so, add the protocol.
/// Go to the basepath if that's set too.
fn open_web(&self, _envs: Vec<(&str, String)>, address: SocketAddr) {
fn open_web(&self, address: SocketAddr) {
let base_path = self.app.build.krate.config.web.app.base_path.clone();
let https = self
.app
+3 -1
View File
@@ -231,7 +231,9 @@ pub(crate) async fn serve_all(mut args: ServeArgs) -> Result<()> {
}
ServeUpdate::OpenApp => {
runner.open_existing(&devserver).await;
if let Err(err) = runner.open_existing(&devserver).await {
tracing::error!("Failed to open app: {err}")
}
}
ServeUpdate::Redraw => {
+8 -5
View File
@@ -146,12 +146,15 @@ impl AppRunner {
}
/// Open an existing app bundle, if it exists
pub(crate) async fn open_existing(&self, devserver: &WebServer) {
if let Some(address) = devserver.server_address() {
let url = format!("http://{address}");
tracing::debug!("opening url: {url}");
_ = open::that(url);
pub(crate) async fn open_existing(&mut self, devserver: &WebServer) -> Result<()> {
if let Some((_, app)) = self
.running
.iter_mut()
.find(|(platform, _)| **platform != Platform::Server)
{
app.open(devserver.devserver_address(), None, true).await?;
}
Ok(())
}
pub(crate) fn attempt_hot_reload(