ssr.rs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //! Example: SSR
  2. //!
  3. //! This example shows how we can render the Dioxus Virtualdom using SSR.
  4. //! Dioxus' SSR is quite comprehensive and can generate a number of utility markers for things like hydration.
  5. //!
  6. //! You can also render without any markers to get a clean HTML output.
  7. use dioxus::prelude::*;
  8. fn main() {
  9. // We can render VirtualDoms
  10. let vdom = VirtualDom::prebuilt(app);
  11. println!("{}", dioxus_ssr::render(&vdom));
  12. // Or we can render rsx! calls themselves
  13. println!(
  14. "{}",
  15. dioxus_ssr::render_element(rsx! {
  16. div {
  17. h1 { "Hello, world!" }
  18. }
  19. })
  20. );
  21. // We can configure the SSR rendering to add ids for rehydration
  22. println!("{}", dioxus_ssr::pre_render(&vdom));
  23. // We can render to a buf directly too
  24. let mut file = String::new();
  25. let mut renderer = dioxus_ssr::Renderer::default();
  26. renderer.render_to(&mut file, &vdom).unwrap();
  27. println!("{file}");
  28. }
  29. fn app() -> Element {
  30. rsx!(
  31. div {
  32. h1 { "Title" }
  33. p { "Body" }
  34. }
  35. )
  36. }