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

+73 -3
View File
@@ -12,6 +12,29 @@ pub const ASSET_ROOT_ENV: &str = "DIOXUS_ASSET_ROOT";
pub const APP_TITLE_ENV: &str = "DIOXUS_APP_TITLE";
pub const OUT_DIR: &str = "DIOXUS_OUT_DIR";
/// Reads an environment variable at runtime in debug mode or at compile time in
/// release mode. When bundling in release mode, we will not be running under the
/// environment variables that the CLI sets, so we need to read them at compile time.
macro_rules! read_env_config {
($name:expr) => {{
#[cfg(debug_assertions)]
{
// In debug mode, read the environment variable set by the CLI at runtime
std::env::var($name).ok()
}
#[cfg(not(debug_assertions))]
{
// In release mode, read the environment variable set by the CLI at compile time
// This means the value will still be available when running the application
// standalone.
// We don't always read the environment variable at compile time to avoid rebuilding
// this crate when the environment variable changes.
option_env!($name).map(ToString::to_string)
}
}};
}
/// Get the address of the devserver for use over a raw socket
///
/// This is not a websocket! There's no protocol!
@@ -51,7 +74,7 @@ pub fn fullstack_address_or_localhost() -> SocketAddr {
}
pub fn app_title() -> Option<String> {
std::env::var(APP_TITLE_ENV).ok()
read_env_config!("DIOXUS_APP_TITLE")
}
pub fn always_on_top() -> Option<bool> {
@@ -64,8 +87,55 @@ pub fn is_cli_enabled() -> bool {
std::env::var(CLI_ENABLED_ENV).is_ok()
}
pub fn base_path() -> Option<PathBuf> {
std::env::var("DIOXUS_ASSET_ROOT").ok().map(PathBuf::from)
#[cfg(feature = "web")]
#[wasm_bindgen::prelude::wasm_bindgen(inline_js = r#"
export function getMetaContents(meta_name) {
const selector = document.querySelector(`meta[name="${meta_name}"]`);
if (!selector) {
return null;
}
return selector.content;
}
"#)]
extern "C" {
#[wasm_bindgen(js_name = getMetaContents)]
pub fn get_meta_contents(selector: &str) -> Option<String>;
}
/// Get the path where the application will be served from. This is used by the router to format the URLs.
pub fn base_path() -> Option<String> {
// This may trigger when compiling to the server if you depend on another crate that pulls in
// the web feature. It might be better for the renderers to provide the current platform
// as a global context
#[cfg(all(feature = "web", target_arch = "wasm32"))]
{
return web_base_path();
}
read_env_config!("DIOXUS_ASSET_ROOT")
}
/// Get the path where the application is served from in the browser.
#[cfg(feature = "web")]
pub fn web_base_path() -> Option<String> {
// In debug mode, we get the base path from the meta element which can be hot reloaded and changed without recompiling
#[cfg(debug_assertions)]
{
thread_local! {
static BASE_PATH: std::cell::OnceCell<Option<String>> = const { std::cell::OnceCell::new() };
}
BASE_PATH.with(|f| f.get_or_init(|| get_meta_contents(ASSET_ROOT_ENV)).clone())
}
// In release mode, we get the base path from the environment variable
#[cfg(not(debug_assertions))]
{
option_env!("DIOXUS_ASSET_ROOT").map(ToString::to_string)
}
}
pub fn format_base_path_meta_element(base_path: &str) -> String {
format!(r#"<meta name="{ASSET_ROOT_ENV}" content="{base_path}">"#,)
}
pub fn out_dir() -> Option<PathBuf> {