1
0
Эх сурвалжийг харах

allow passing props to the rendered component

Evan Almloff 2 жил өмнө
parent
commit
f618da7311

+ 2 - 1
Cargo.toml

@@ -23,7 +23,8 @@ members = [
     "packages/hot-reload",
     "packages/server",
     "packages/server/server-macro",
-    "packages/server/examples/hello-world",
+    "packages/server/examples/axum-hello-world",
+    "packages/server/examples/salvo-hello-world",
     "docs/guide",
 ]
 

+ 3 - 3
packages/server/Cargo.toml

@@ -13,12 +13,12 @@ server_macro = { path = "server-macro" }
 warp = { version = "0.3.3", optional = true }
 
 # axum
-axum = { version = "0.6.1", optional = true, features = ["ws"] }
+axum = { version = "0.6.1", optional = true }
 tower-http = { version = "0.4.0", optional = true, features = ["fs"] }
 hyper = { version = "0.14.25", optional = true }
 
 # salvo
-salvo = { version = "0.37.7", optional = true, features = ["ws"] }
+salvo = { version = "0.37.7", optional = true, features = ["serve-static"] }
 serde = "1.0.159"
 
 dioxus-core = { path = "../core", version = "^0.3.0" }
@@ -33,5 +33,5 @@ tokio = { version = "1.27.0", features = ["full"], optional = true }
 default = []
 warp = ["dep:warp"]
 axum = ["dep:axum", "tower-http", "hyper"]
-salvo = ["dep:salvo"]
+salvo = ["dep:salvo", "hyper"]
 ssr = ["server_fn/ssr", "tokio", "dioxus-ssr"]

+ 2 - 0
packages/server/src/adapters/mod.rs

@@ -1,2 +1,4 @@
 #[cfg(feature = "axum")]
 pub mod axum_adapter;
+#[cfg(feature = "salvo")]
+pub mod salvo_adapter;

+ 5 - 2
packages/server/src/lib.rs

@@ -9,6 +9,8 @@ mod server_fn;
 pub mod prelude {
     #[cfg(feature = "axum")]
     pub use crate::adapters::axum_adapter::*;
+    #[cfg(feature = "salvo")]
+    pub use crate::adapters::salvo_adapter::*;
     #[cfg(feature = "ssr")]
     pub use crate::serve::ServeConfig;
     pub use crate::server_fn::{DioxusServerContext, ServerFn};
@@ -17,7 +19,7 @@ pub mod prelude {
 }
 
 #[cfg(feature = "ssr")]
-fn dioxus_ssr_html(cfg: serve::ServeConfig) -> String {
+fn dioxus_ssr_html<P: 'static + Clone>(cfg: &serve::ServeConfig<P>) -> String {
     use prelude::ServeConfig;
 
     let ServeConfig {
@@ -25,12 +27,13 @@ fn dioxus_ssr_html(cfg: serve::ServeConfig) -> String {
         application_name,
         base_path,
         head,
+        props,
         ..
     } = cfg;
 
     let application_name = application_name.unwrap_or("dioxus");
 
-    let mut vdom = VirtualDom::new(app);
+    let mut vdom = VirtualDom::new_with_props(*app, props.clone());
     let _ = vdom.rebuild();
     let renderered = dioxus_ssr::pre_render(&vdom);
     let base_path = base_path.unwrap_or(".");

+ 6 - 4
packages/server/src/serve.rs

@@ -1,19 +1,21 @@
 use dioxus_core::Component;
 
 #[derive(Clone)]
-pub struct ServeConfig {
-    pub(crate) app: Component,
+pub struct ServeConfig<P: Clone> {
+    pub(crate) app: Component<P>,
+    pub(crate) props: P,
     pub(crate) application_name: Option<&'static str>,
     pub(crate) server_fn_route: Option<&'static str>,
     pub(crate) base_path: Option<&'static str>,
     pub(crate) head: Option<&'static str>,
 }
 
-impl ServeConfig {
+impl<P: Clone> ServeConfig<P> {
     /// Create a new ServeConfig
-    pub fn new(app: Component) -> Self {
+    pub fn new(app: Component<P>, props: P) -> Self {
         Self {
             app,
+            props,
             application_name: None,
             server_fn_route: None,
             base_path: None,

+ 1 - 0
packages/server/src/server_fn.rs

@@ -1,3 +1,4 @@
+#[derive(Clone)]
 pub struct DioxusServerContext {}
 
 #[cfg(any(feature = "ssr", doc))]