2022-01-03 05:42:17 +00:00
|
|
|
//! Example: SSR
|
|
|
|
//!
|
|
|
|
//! This example shows how we can render the Dioxus Virtualdom using SSR.
|
2024-02-14 21:48:58 +00:00
|
|
|
//! Dioxus' SSR is quite comprehensive and can generate a number of utility markers for things like hydration.
|
|
|
|
//!
|
|
|
|
//! You can also render without any markers to get a clean HTML output.
|
2022-01-03 05:42:17 +00:00
|
|
|
|
2024-01-16 05:12:44 +00:00
|
|
|
use dioxus::prelude::*;
|
2021-07-11 23:31:07 +00:00
|
|
|
|
|
|
|
fn main() {
|
2022-01-03 05:42:17 +00:00
|
|
|
// We can render VirtualDoms
|
2024-01-31 01:33:14 +00:00
|
|
|
let vdom = VirtualDom::prebuilt(app);
|
2022-12-07 21:11:40 +00:00
|
|
|
println!("{}", dioxus_ssr::render(&vdom));
|
2022-01-03 05:42:17 +00:00
|
|
|
|
2024-01-16 19:18:46 +00:00
|
|
|
// Or we can render rsx! calls themselves
|
2022-01-03 05:42:17 +00:00
|
|
|
println!(
|
|
|
|
"{}",
|
2024-01-16 19:18:46 +00:00
|
|
|
dioxus_ssr::render_element(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-16 19:18:46 +00:00
|
|
|
rsx!(
|
2021-07-11 23:31:07 +00:00
|
|
|
div {
|
|
|
|
h1 { "Title" }
|
|
|
|
p { "Body" }
|
|
|
|
}
|
2024-01-15 19:54:17 +00:00
|
|
|
)
|
2022-01-02 23:35:38 +00:00
|
|
|
}
|