feat: overhaul ssr

This commit is contained in:
Jonathan Kelley committed 2022-12-06 17:41:47 -08:00
1 parent e8ae1fb83e
commit 34d9aafe0e
16 files changed
+263 -561

No files matched your search

+40 -3
View File
@@ -2,8 +2,45 @@
mod cache;
pub mod config;
pub mod helpers;
pub mod renderer;
pub mod template;
pub use helpers::*;
pub use template::SsrRender;
use dioxus_core::{Element, LazyNodes, Scope, VirtualDom};
use std::cell::Cell;
use crate::renderer::Renderer;
/// A convenience function to render an `rsx!` call to a string
///
/// For advanced rendering, create a new `SsrRender`.
pub fn render_lazy<'a, 'b>(f: LazyNodes<'a, 'b>) -> String {
// We need to somehow get the lazy call into the virtualdom even with the lifetime
// Since the lazy lifetime is valid for this function, we can just transmute it to static temporarily
// This is okay since we're returning an owned value
struct RootProps<'a, 'b> {
caller: Cell<Option<LazyNodes<'a, 'b>>>,
}
fn lazy_app<'a>(cx: Scope<'a, RootProps<'static, 'static>>) -> Element<'a> {
let lazy = cx.props.caller.take().unwrap();
let lazy: LazyNodes = unsafe { std::mem::transmute(lazy) };
Ok(lazy.call(cx))
}
let props: RootProps = unsafe {
std::mem::transmute(RootProps {
caller: Cell::new(Some(f)),
})
};
let mut dom = VirtualDom::new_with_props(lazy_app, props);
_ = dom.rebuild();
Renderer::new().render(&dom)
}
/// A convenience function to render an existing VirtualDom to a string
///
/// We generally recommend creating a new `Renderer` to take advantage of template caching.
pub fn render(dom: &VirtualDom) -> String {
Renderer::new().render(dom)
}