2022-01-03 05:42:17 +00:00
|
|
|
//! Example: SSR
|
|
|
|
//!
|
|
|
|
//! This example shows how we can render the Dioxus Virtualdom using SSR.
|
|
|
|
|
|
|
|
use std::fmt::Write;
|
|
|
|
|
2021-07-11 23:31:07 +00:00
|
|
|
use dioxus::prelude::*;
|
2022-11-16 07:22:41 +00:00
|
|
|
use dioxus_ssr::config::SsrConfig;
|
2021-07-11 23:31:07 +00:00
|
|
|
|
|
|
|
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-07-09 19:15:20 +00:00
|
|
|
println!("{}", dioxus_ssr::render_vdom(&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
|
|
|
|
println!(
|
|
|
|
"{}",
|
2022-11-16 07:22:41 +00:00
|
|
|
dioxus_ssr::render_vdom_cfg(&vdom, SsrConfig::default().pre_render(true))
|
2022-01-03 05:42:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// We can even render as a writer
|
|
|
|
let mut file = String::new();
|
|
|
|
let _ = file.write_fmt(format_args!(
|
|
|
|
"{}",
|
2022-11-16 07:22:41 +00:00
|
|
|
dioxus_ssr::SsrRender::default().render_vdom(&vdom)
|
2022-01-03 05:42:17 +00:00
|
|
|
));
|
|
|
|
println!("{}", file);
|
2021-07-11 23:31:07 +00:00
|
|
|
}
|
|
|
|
|
2022-01-02 23:35:38 +00:00
|
|
|
fn app(cx: Scope) -> Element {
|
2021-07-11 23:31:07 +00:00
|
|
|
cx.render(rsx!(
|
|
|
|
div {
|
|
|
|
h1 { "Title" }
|
|
|
|
p { "Body" }
|
|
|
|
}
|
|
|
|
))
|
2022-01-02 23:35:38 +00:00
|
|
|
}
|