2022-01-03 05:42:17 +00:00
|
|
|
//! Example: SSR
|
|
|
|
//!
|
|
|
|
//! This example shows how we can render the Dioxus Virtualdom using SSR.
|
|
|
|
|
2021-07-11 23:31:07 +00:00
|
|
|
use dioxus::prelude::*;
|
|
|
|
|
|
|
|
fn main() {
|
2022-01-03 05:42:17 +00:00
|
|
|
// We can render VirtualDoms
|
2022-01-02 23:35:38 +00:00
|
|
|
let mut vdom = VirtualDom::new(app);
|
2021-12-25 22:18:05 +00:00
|
|
|
let _ = vdom.rebuild();
|
2022-12-07 21:11:40 +00:00
|
|
|
println!("{}", dioxus_ssr::render(&vdom));
|
2022-01-03 05:42:17 +00:00
|
|
|
|
|
|
|
// Or we can render rsx! calls themselves
|
|
|
|
println!(
|
|
|
|
"{}",
|
2022-07-09 19:15:20 +00:00
|
|
|
dioxus_ssr::render_lazy(rsx! {
|
2022-01-03 05:42:17 +00:00
|
|
|
div {
|
|
|
|
h1 { "Hello, world!" }
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
// We can configure the SSR rendering to add ids for rehydration
|
2022-12-07 21:11:40 +00:00
|
|
|
println!("{}", dioxus_ssr::pre_render(&vdom));
|
2022-01-03 05:42:17 +00:00
|
|
|
|
2022-12-07 21:11:40 +00:00
|
|
|
// We can render to a buf directly too
|
2022-01-03 05:42:17 +00:00
|
|
|
let mut file = String::new();
|
2022-12-07 21:11:40 +00:00
|
|
|
let mut renderer = dioxus_ssr::Renderer::default();
|
|
|
|
renderer.render_to(&mut file, &vdom).unwrap();
|
2023-01-28 02:35:46 +00:00
|
|
|
println!("{file}");
|
2021-07-11 23:31:07 +00:00
|
|
|
}
|
|
|
|
|
2024-01-14 04:51:37 +00:00
|
|
|
fn app() -> Element {
|
2024-01-14 05:12:21 +00:00
|
|
|
rsx!(
|
2021-07-11 23:31:07 +00:00
|
|
|
div {
|
|
|
|
h1 { "Title" }
|
|
|
|
p { "Body" }
|
|
|
|
}
|
|
|
|
))
|
2022-01-02 23:35:38 +00:00
|
|
|
}
|