Add liveview support to the CLI and make fullstack runnable from dist (#2759)
* add liveview cli support * Fix TUI fullstack deadlock * look for fullstack assets in the public directory * Fix fullstack with the CLI * Fix static generation server
This commit is contained in:
1 parent
9dbdf74a1e
commit
e5e578d27b
14 files changed
+428
-272
No files matched your search
@@ -125,7 +125,7 @@ pub trait DioxusRouterExt<S> {
|
||||
/// let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 8080));
|
||||
/// let router = axum::Router::new()
|
||||
/// // Server side render the application, serve static assets, and register server functions
|
||||
/// .serve_static_assets("dist")
|
||||
/// .serve_static_assets()
|
||||
/// // Server render the application
|
||||
/// // ...
|
||||
/// .into_make_service();
|
||||
@@ -133,7 +133,7 @@ pub trait DioxusRouterExt<S> {
|
||||
/// axum::serve(listener, router).await.unwrap();
|
||||
/// }
|
||||
/// ```
|
||||
fn serve_static_assets(self, assets_path: impl Into<std::path::PathBuf>) -> Self
|
||||
fn serve_static_assets(self) -> Self
|
||||
where
|
||||
Self: Sized;
|
||||
|
||||
@@ -203,18 +203,16 @@ where
|
||||
self
|
||||
}
|
||||
|
||||
// TODO: This is a breaking change, but we should probably serve static assets from a different directory than dist where the server executable is located
|
||||
// This would prevent issues like https://github.com/DioxusLabs/dioxus/issues/2327
|
||||
fn serve_static_assets(mut self, assets_path: impl Into<std::path::PathBuf>) -> Self {
|
||||
fn serve_static_assets(mut self) -> Self {
|
||||
use tower_http::services::{ServeDir, ServeFile};
|
||||
|
||||
let assets_path = assets_path.into();
|
||||
let public_path = crate::public_path();
|
||||
|
||||
// Serve all files in dist folder except index.html
|
||||
let dir = std::fs::read_dir(&assets_path).unwrap_or_else(|e| {
|
||||
// Serve all files in public folder except index.html
|
||||
let dir = std::fs::read_dir(&public_path).unwrap_or_else(|e| {
|
||||
panic!(
|
||||
"Couldn't read assets directory at {:?}: {}",
|
||||
&assets_path, e
|
||||
"Couldn't read public directory at {:?}: {}",
|
||||
&public_path, e
|
||||
)
|
||||
});
|
||||
|
||||
@@ -224,7 +222,7 @@ where
|
||||
continue;
|
||||
}
|
||||
let route = path
|
||||
.strip_prefix(&assets_path)
|
||||
.strip_prefix(&public_path)
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|segment| {
|
||||
@@ -251,10 +249,7 @@ where
|
||||
let ssr_state = SSRState::new(&cfg);
|
||||
|
||||
// Add server functions and render index.html
|
||||
#[allow(unused_mut)]
|
||||
let mut server = self
|
||||
.serve_static_assets(cfg.assets_path.clone())
|
||||
.register_server_functions();
|
||||
let server = self.serve_static_assets().register_server_functions();
|
||||
|
||||
server.fallback(
|
||||
get(render_handler).with_state(
|
||||
|
||||
@@ -155,7 +155,7 @@ async fn launch_server(
|
||||
|
||||
let cfg = platform_config.server_cfg.build();
|
||||
|
||||
let mut router = router.serve_static_assets(cfg.assets_path.clone());
|
||||
let mut router = router.serve_static_assets();
|
||||
|
||||
router.fallback(
|
||||
axum::routing::get(crate::axum_adapter::render_handler).with_state(
|
||||
|
||||
@@ -11,7 +11,6 @@ pub struct ServeConfigBuilder {
|
||||
pub(crate) root_id: Option<&'static str>,
|
||||
pub(crate) index_html: Option<String>,
|
||||
pub(crate) index_path: Option<PathBuf>,
|
||||
pub(crate) assets_path: Option<PathBuf>,
|
||||
pub(crate) incremental: Option<dioxus_ssr::incremental::IncrementalRendererConfig>,
|
||||
}
|
||||
|
||||
@@ -22,7 +21,6 @@ impl ServeConfigBuilder {
|
||||
root_id: None,
|
||||
index_html: None,
|
||||
index_path: None,
|
||||
assets_path: None,
|
||||
incremental: None,
|
||||
}
|
||||
}
|
||||
@@ -51,25 +49,15 @@ impl ServeConfigBuilder {
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the path of the assets folder generated by the Dioxus CLI. (defaults to dist)
|
||||
pub fn assets_path(mut self, assets_path: PathBuf) -> Self {
|
||||
self.assets_path = Some(assets_path);
|
||||
self
|
||||
}
|
||||
|
||||
/// Build the ServeConfig
|
||||
pub fn build(self) -> ServeConfig {
|
||||
let assets_path = self.assets_path.unwrap_or(
|
||||
dioxus_cli_config::CURRENT_CONFIG
|
||||
.as_ref()
|
||||
.map(|c| c.application.out_dir.clone())
|
||||
.unwrap_or("dist".into()),
|
||||
);
|
||||
// The CLI always bundles static assets into the exe/public directory
|
||||
let public_path = public_path();
|
||||
|
||||
let index_path = self
|
||||
.index_path
|
||||
.map(PathBuf::from)
|
||||
.unwrap_or_else(|| assets_path.join("index.html"));
|
||||
.unwrap_or_else(|| public_path.join("index.html"));
|
||||
|
||||
let root_id = self.root_id.unwrap_or("main");
|
||||
|
||||
@@ -81,14 +69,23 @@ impl ServeConfigBuilder {
|
||||
|
||||
ServeConfig {
|
||||
index,
|
||||
assets_path,
|
||||
incremental: self.incremental,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the path to the public assets directory to serve static files from
|
||||
pub(crate) fn public_path() -> PathBuf {
|
||||
// The CLI always bundles static assets into the exe/public directory
|
||||
std::env::current_exe()
|
||||
.expect("Failed to get current executable path")
|
||||
.parent()
|
||||
.unwrap()
|
||||
.join("public")
|
||||
}
|
||||
|
||||
fn load_index_path(path: PathBuf) -> String {
|
||||
let mut file = File::open(path).expect("Failed to find index.html. Make sure the index_path is set correctly and the WASM application has been built.");
|
||||
let mut file = File::open(&path).unwrap_or_else(|_| panic!("Failed to find index.html. Make sure the index_path is set correctly and the WASM application has been built. Tried to open file at path: {path:?}"));
|
||||
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents)
|
||||
@@ -156,8 +153,6 @@ pub(crate) struct IndexHtml {
|
||||
#[derive(Clone)]
|
||||
pub struct ServeConfig {
|
||||
pub(crate) index: IndexHtml,
|
||||
#[allow(unused)]
|
||||
pub(crate) assets_path: PathBuf,
|
||||
pub(crate) incremental: Option<dioxus_ssr::incremental::IncrementalRendererConfig>,
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user