lib.rs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #![doc = include_str!("../README.md")]
  2. #![doc(html_logo_url = "https://avatars.githubusercontent.com/u/79236386")]
  3. #![doc(html_favicon_url = "https://avatars.githubusercontent.com/u/79236386")]
  4. mod cache;
  5. pub mod config;
  6. mod fs_cache;
  7. #[cfg(feature = "incremental")]
  8. pub mod incremental;
  9. #[cfg(feature = "incremental")]
  10. mod incremental_cfg;
  11. pub mod renderer;
  12. pub mod template;
  13. use dioxus_core::{Element, LazyNodes, Scope, VirtualDom};
  14. use std::cell::Cell;
  15. pub use crate::renderer::Renderer;
  16. /// A convenience function to render an `rsx!` call to a string
  17. ///
  18. /// For advanced rendering, create a new `SsrRender`.
  19. pub fn render_lazy(f: LazyNodes<'_, '_>) -> String {
  20. // We need to somehow get the lazy call into the virtualdom even with the lifetime
  21. // Since the lazy lifetime is valid for this function, we can just transmute it to static temporarily
  22. // This is okay since we're returning an owned value
  23. struct RootProps<'a, 'b> {
  24. caller: Cell<Option<LazyNodes<'a, 'b>>>,
  25. }
  26. fn lazy_app<'a>(cx: Scope<'a, RootProps<'static, 'static>>) -> Element<'a> {
  27. let lazy = cx.props.caller.take().unwrap();
  28. let lazy: LazyNodes = unsafe { std::mem::transmute(lazy) };
  29. Some(lazy.call(cx))
  30. }
  31. let props: RootProps = unsafe {
  32. std::mem::transmute(RootProps {
  33. caller: Cell::new(Some(f)),
  34. })
  35. };
  36. let mut dom = VirtualDom::new_with_props(lazy_app, props);
  37. _ = dom.rebuild();
  38. Renderer::new().render(&dom)
  39. }
  40. /// A convenience function to render an existing VirtualDom to a string
  41. ///
  42. /// We generally recommend creating a new `Renderer` to take advantage of template caching.
  43. pub fn render(dom: &VirtualDom) -> String {
  44. Renderer::new().render(dom)
  45. }
  46. /// A convenience function to pre-render an existing VirtualDom to a string
  47. ///
  48. /// We generally recommend creating a new `Renderer` to take advantage of template caching.
  49. pub fn pre_render(dom: &VirtualDom) -> String {
  50. let mut renderer = Renderer::new();
  51. renderer.pre_render = true;
  52. renderer.render(dom)
  53. }