add an example illistrating intigration with the router
This commit is contained in:
1 parent
976d4ab960
commit
bfcb0f6eab
15 files changed
+283
-72
No files matched your search
@@ -2,6 +2,7 @@ use std::{error::Error, sync::Arc};
|
||||
|
||||
use axum::{
|
||||
body::{self, Body, BoxBody, Full},
|
||||
extract::{FromRef, State},
|
||||
http::{HeaderMap, Request, Response, StatusCode},
|
||||
response::IntoResponse,
|
||||
routing::{get, post},
|
||||
@@ -11,7 +12,7 @@ use server_fn::{Payload, ServerFunctionRegistry};
|
||||
use tokio::task::spawn_blocking;
|
||||
|
||||
use crate::{
|
||||
dioxus_ssr_html,
|
||||
render::SSRState,
|
||||
serve::ServeConfig,
|
||||
server_fn::{DioxusServerContext, DioxusServerFnRegistry, ServerFnTraitObj},
|
||||
};
|
||||
@@ -21,10 +22,15 @@ pub trait DioxusRouterExt {
|
||||
fn serve_dioxus_application<P: Clone + Send + Sync + 'static>(
|
||||
self,
|
||||
cfg: ServeConfig<P>,
|
||||
server_fn_route: Option<&'static str>,
|
||||
) -> Self;
|
||||
}
|
||||
|
||||
impl DioxusRouterExt for Router {
|
||||
impl<S> DioxusRouterExt for Router<S>
|
||||
where
|
||||
SSRState: FromRef<S>,
|
||||
S: Send + Sync + Clone + 'static,
|
||||
{
|
||||
fn register_server_fns(self, server_fn_route: &'static str) -> Self {
|
||||
let mut router = self;
|
||||
for server_fn_path in DioxusServerFnRegistry::paths_registered() {
|
||||
@@ -43,17 +49,18 @@ impl DioxusRouterExt for Router {
|
||||
fn serve_dioxus_application<P: Clone + Send + Sync + 'static>(
|
||||
self,
|
||||
cfg: ServeConfig<P>,
|
||||
server_fn_route: Option<&'static str>,
|
||||
) -> Self {
|
||||
use tower_http::services::ServeDir;
|
||||
|
||||
// Serve the dist folder and the index.html file
|
||||
let serve_dir = ServeDir::new("dist");
|
||||
|
||||
self.register_server_fns(cfg.server_fn_route.unwrap_or_default())
|
||||
self.register_server_fns(server_fn_route.unwrap_or_default())
|
||||
.route(
|
||||
"/",
|
||||
get(move || {
|
||||
let rendered = dioxus_ssr_html(&cfg);
|
||||
get(move |State(ssr_state): State<SSRState>| {
|
||||
let rendered = ssr_state.render(&cfg);
|
||||
async move { Full::from(rendered) }
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -3,6 +3,8 @@ use dioxus_core::prelude::*;
|
||||
|
||||
mod adapters;
|
||||
#[cfg(feature = "ssr")]
|
||||
pub mod render;
|
||||
#[cfg(feature = "ssr")]
|
||||
mod serve;
|
||||
mod server_fn;
|
||||
|
||||
@@ -14,57 +16,10 @@ pub mod prelude {
|
||||
#[cfg(feature = "warp")]
|
||||
pub use crate::adapters::warp_adapter::*;
|
||||
#[cfg(feature = "ssr")]
|
||||
pub use crate::render::*;
|
||||
#[cfg(feature = "ssr")]
|
||||
pub use crate::serve::ServeConfig;
|
||||
pub use crate::server_fn::{DioxusServerContext, ServerFn};
|
||||
pub use server_fn::{self, ServerFn as _, ServerFnError};
|
||||
pub use server_macro::*;
|
||||
}
|
||||
|
||||
#[cfg(feature = "ssr")]
|
||||
fn dioxus_ssr_html<P: 'static + Clone>(cfg: &serve::ServeConfig<P>) -> String {
|
||||
use prelude::ServeConfig;
|
||||
|
||||
let ServeConfig {
|
||||
app,
|
||||
application_name,
|
||||
base_path,
|
||||
head,
|
||||
props,
|
||||
..
|
||||
} = cfg;
|
||||
|
||||
let application_name = application_name.unwrap_or("dioxus");
|
||||
|
||||
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(".");
|
||||
let head = head.unwrap_or(
|
||||
r#"<title>Dioxus Application</title>
|
||||
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<meta charset="UTF-8" />"#,
|
||||
);
|
||||
format!(
|
||||
r#"
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
{head}
|
||||
</head>
|
||||
<body>
|
||||
<div id="main">
|
||||
{renderered}
|
||||
</div>
|
||||
<script type="module">
|
||||
import init from "/{base_path}/assets/dioxus/{application_name}.js";
|
||||
init("/{base_path}/assets/dioxus/{application_name}_bg.wasm").then(wasm => {{
|
||||
if (wasm.__wbindgen_start == undefined) {{
|
||||
wasm.main();
|
||||
}}
|
||||
}});
|
||||
</script>
|
||||
</body>
|
||||
</html>"#
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
use std::fmt::Write;
|
||||
use std::sync::Arc;
|
||||
|
||||
use dioxus_core::VirtualDom;
|
||||
use dioxus_ssr::Renderer;
|
||||
|
||||
use crate::prelude::ServeConfig;
|
||||
|
||||
fn dioxus_ssr_html<P: 'static + Clone>(cfg: &ServeConfig<P>, renderer: &mut Renderer) -> String {
|
||||
let ServeConfig {
|
||||
app,
|
||||
application_name,
|
||||
base_path,
|
||||
head,
|
||||
props,
|
||||
..
|
||||
} = cfg;
|
||||
|
||||
let application_name = application_name.unwrap_or("dioxus");
|
||||
let mut vdom = VirtualDom::new_with_props(*app, props.clone());
|
||||
let _ = vdom.rebuild();
|
||||
let base_path = base_path.unwrap_or(".");
|
||||
let head = head.unwrap_or(
|
||||
r#"<title>Dioxus Application</title>
|
||||
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<meta charset="UTF-8" />"#,
|
||||
);
|
||||
|
||||
let mut html = String::new();
|
||||
|
||||
if let Err(err) = write!(
|
||||
&mut html,
|
||||
r#"
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>{head}
|
||||
</head>
|
||||
<body>
|
||||
<div id="main">"#
|
||||
) {
|
||||
eprintln!("Failed to write to html: {}", err);
|
||||
}
|
||||
|
||||
let _ = renderer.render_to(&mut html, &vdom);
|
||||
|
||||
if let Err(err) = write!(
|
||||
&mut html,
|
||||
r#"</div>
|
||||
<script type="module">
|
||||
import init from "/{base_path}/assets/dioxus/{application_name}.js";
|
||||
init("/{base_path}/assets/dioxus/{application_name}_bg.wasm").then(wasm => {{
|
||||
if (wasm.__wbindgen_start == undefined) {{
|
||||
wasm.main();
|
||||
}}
|
||||
}});
|
||||
</script>
|
||||
</body>
|
||||
</html>"#
|
||||
) {
|
||||
eprintln!("Failed to write to html: {}", err);
|
||||
}
|
||||
|
||||
html
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SSRState {
|
||||
// We keep a cache of renderers to avoid re-creating them on every request. They are boxed to make them very cheap to move
|
||||
renderers: Arc<object_pool::Pool<Renderer>>,
|
||||
}
|
||||
|
||||
impl Default for SSRState {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
renderers: Arc::new(object_pool::Pool::new(10, pre_renderer)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SSRState {
|
||||
pub fn render<P: 'static + Clone>(&self, cfg: &ServeConfig<P>) -> String {
|
||||
let mut renderer = self.renderers.pull(pre_renderer);
|
||||
dioxus_ssr_html(cfg, &mut renderer)
|
||||
}
|
||||
}
|
||||
|
||||
fn pre_renderer() -> Renderer {
|
||||
let mut renderer = Renderer::default();
|
||||
renderer.pre_render = true;
|
||||
renderer
|
||||
}
|
||||
@@ -5,7 +5,6 @@ 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>,
|
||||
}
|
||||
@@ -17,7 +16,6 @@ impl<P: Clone> ServeConfig<P> {
|
||||
app,
|
||||
props,
|
||||
application_name: None,
|
||||
server_fn_route: None,
|
||||
base_path: None,
|
||||
head: None,
|
||||
}
|
||||
@@ -29,12 +27,6 @@ impl<P: Clone> ServeConfig<P> {
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the base route all server functions will be served under
|
||||
pub fn server_fn_route(mut self, server_fn_route: &'static str) -> Self {
|
||||
self.server_fn_route = Some(server_fn_route);
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the path the WASM application will be served under
|
||||
pub fn base_path(mut self, base_path: &'static str) -> Self {
|
||||
self.base_path = Some(base_path);
|
||||
|
||||
Reference in new issue
Block a user