瀏覽代碼

Enable serve_dioxus_application for WASM targets (#4170)

* optional asset serving

* optional asset serving

* Add serve_api_application for WASM environments

* fix cfg

* remove old placeholder
Drew Ridley 1 月之前
父節點
當前提交
eb15b42ab1
共有 1 個文件被更改,包括 35 次插入0 次删除
  1. 35 0
      packages/server/src/server.rs

+ 35 - 0
packages/server/src/server.rs

@@ -177,6 +177,27 @@ pub trait DioxusRouterFnExt<S> {
     /// }
     /// }
     /// ```
     /// ```
     fn register_server_functions_with_context(self, context_providers: ContextProviders) -> Self;
     fn register_server_functions_with_context(self, context_providers: ContextProviders) -> Self;
+
+    /// Serves a Dioxus application without static assets.
+    /// Sets up server function routes and rendering endpoints only.
+    ///
+    /// Useful for WebAssembly environments or when static assets
+    /// are served by another system.
+    ///
+    /// # Example
+    /// ```rust, no_run
+    /// # use dioxus_lib::prelude::*;
+    /// # use dioxus_fullstack::prelude::*;
+    /// #[tokio::main]
+    /// async fn main() {
+    ///     let router = axum::Router::new()
+    ///         .serve_api_application(ServeConfig::new().unwrap(), app)
+    ///         .into_make_service();
+    ///     // ...
+    /// }
+    fn serve_api_application(self, cfg: ServeConfig, app: fn() -> Element) -> Self
+    where
+        Self: Sized;
 }
 }
 
 
 impl<S> DioxusRouterFnExt<S> for Router<S>
 impl<S> DioxusRouterFnExt<S> for Router<S>
@@ -192,6 +213,20 @@ where
         }
         }
         self
         self
     }
     }
+
+    fn serve_api_application(self, cfg: ServeConfig, app: fn() -> Element) -> Self
+    where
+        Self: Sized,
+    {
+        let server = self.register_server_functions_with_context(cfg.context_providers.clone());
+
+        let ssr_state = SSRState::new(&cfg);
+
+        server.fallback(
+            get(render_handler)
+                .with_state(RenderHandleState::new(cfg, app).with_ssr_state(ssr_state)),
+        )
+    }
 }
 }
 
 
 pub fn register_server_fn_on_router<S>(
 pub fn register_server_fn_on_router<S>(