diff --git a/examples/custom_element.rs b/examples/custom_element.rs index 92c61ef86..19bfa0fd9 100644 --- a/examples/custom_element.rs +++ b/examples/custom_element.rs @@ -10,7 +10,7 @@ fn main() { let mut dom = VirtualDom::new(app); let _ = dom.rebuild(); - let output = dioxus_ssr::render_vdom(&dom); + let output = dioxus_ssr::render(&dom); println!("{}", output); } diff --git a/examples/rsx_compile_fail.rs b/examples/rsx_compile_fail.rs index 500cdad84..375219609 100644 --- a/examples/rsx_compile_fail.rs +++ b/examples/rsx_compile_fail.rs @@ -2,14 +2,14 @@ //! It also proves that lifetimes work properly, especially when used with use_ref use dioxus::prelude::*; -use dioxus_ssr::config::Config; fn main() { let mut vdom = VirtualDom::new(example); _ = vdom.rebuild(); - let out = dioxus_ssr::render_vdom_cfg(&vdom, Config::default().newline(true).indent(true)); - println!("{}", out); + let mut renderer = dioxus_ssr::Renderer::new(); + renderer.pretty = true; + renderer.render(&vdom); } fn example(cx: Scope) -> Element { diff --git a/packages/hooks/src/usecallback.rs b/packages/hooks/src/usecallback.rs index 0c5661a92..3f223b581 100644 --- a/packages/hooks/src/usecallback.rs +++ b/packages/hooks/src/usecallback.rs @@ -24,7 +24,7 @@ macro_rules! use_callback { ) }; } -pub fn use_callback<'a, T, R, F>(cx: &'a ScopeState, make: impl FnOnce() -> R) -> impl FnMut(T) + 'a +pub fn use_callback(cx: &ScopeState, make: impl FnOnce() -> R) -> impl FnMut(T) + '_ where R: FnMut(T) -> F + 'static, F: Future + 'static, diff --git a/packages/hooks/src/usefuture.rs b/packages/hooks/src/usefuture.rs index 9063c7db6..df34a6433 100644 --- a/packages/hooks/src/usefuture.rs +++ b/packages/hooks/src/usefuture.rs @@ -51,7 +51,7 @@ where // Clone in our cells let values = state.values.clone(); - let _schedule_update = state.update.clone(); + let schedule_update = state.update.clone(); let waker = state.waker.clone(); // Cancel the current future @@ -66,10 +66,7 @@ where // if there's a waker, we dont re-render the component. Instead we just progress that future match waker.borrow().as_ref() { Some(waker) => waker.wake_by_ref(), - None => { - - // schedule_update() - } + None => schedule_update(), } }))); } diff --git a/packages/router/src/components/link.rs b/packages/router/src/components/link.rs index 4264e1ed0..5098090b2 100644 --- a/packages/router/src/components/link.rs +++ b/packages/router/src/components/link.rs @@ -105,7 +105,7 @@ pub fn Link<'a>(cx: Scope<'a, LinkProps<'a>>) -> Element { } }; - let route = use_route(&cx); + let route = use_route(cx); let url = route.url(); let path = url.path(); let active = path == cx.props.to; diff --git a/packages/router/src/components/redirect.rs b/packages/router/src/components/redirect.rs index 5bb0650e2..3bbe50b7e 100644 --- a/packages/router/src/components/redirect.rs +++ b/packages/router/src/components/redirect.rs @@ -32,7 +32,7 @@ pub struct RedirectProps<'a> { /// /// It will replace the current route rather than pushing the current one to the stack. pub fn Redirect<'a>(cx: Scope<'a, RedirectProps<'a>>) -> Element { - let router = use_router(&cx); + let router = use_router(cx); let immediate_redirect = cx.use_hook(|| { if let Some(from) = cx.props.from { diff --git a/packages/router/src/components/route.rs b/packages/router/src/components/route.rs index 60b85d687..7bfc48993 100644 --- a/packages/router/src/components/route.rs +++ b/packages/router/src/components/route.rs @@ -42,7 +42,7 @@ pub fn Route<'a>(cx: Scope<'a, RouteProps<'a>>) -> Element { }); // submit our rout - router_root.register_total_route(route_context.total_route.clone(), cx.scope_id()); + router_root.register_total_route(route_context.total_route, cx.scope_id()); }); log::trace!("Checking Route: {:?}", cx.props.to); diff --git a/packages/router/src/components/router.rs b/packages/router/src/components/router.rs index 53b4717ad..14a0e14e7 100644 --- a/packages/router/src/components/router.rs +++ b/packages/router/src/components/router.rs @@ -41,7 +41,7 @@ pub struct RouterProps<'a> { pub fn Router<'a>(cx: Scope<'a, RouterProps<'a>>) -> Element { let svc = use_context_provider(cx, || { RouterService::new( - &cx, + cx, RouterCfg { base_url: cx.props.base_url.map(|s| s.to_string()), active_class: cx.props.active_class.map(|s| s.to_string()), diff --git a/packages/rsx/src/component.rs b/packages/rsx/src/component.rs index 090f7382d..2dddb1533 100644 --- a/packages/rsx/src/component.rs +++ b/packages/rsx/src/component.rs @@ -60,7 +60,7 @@ impl Component { match self .fields .iter() - .find(|f| f.name.to_string() == "key") + .find(|f| f.name == "key") .map(|f| &f.content) { Some(ContentField::Formatted(fmt)) => Some(fmt), diff --git a/packages/rsx/src/ifmt.rs b/packages/rsx/src/ifmt.rs index 707141329..e94884907 100644 --- a/packages/rsx/src/ifmt.rs +++ b/packages/rsx/src/ifmt.rs @@ -21,10 +21,7 @@ pub struct IfmtInput { impl IfmtInput { pub fn is_static(&self) -> bool { - match self.segments.as_slice() { - &[Segment::Literal(_)] => true, - _ => false, - } + matches!(self.segments.as_slice(), &[Segment::Literal(_)]) } } diff --git a/packages/rsx/src/lib.rs b/packages/rsx/src/lib.rs index adb36d59b..7cfcc2d9f 100644 --- a/packages/rsx/src/lib.rs +++ b/packages/rsx/src/lib.rs @@ -180,10 +180,10 @@ impl<'a> DynamicContext<'a> { // [0, 2] // [0, 2, 1] - let static_attrs = el.attributes.iter().filter_map(|attr| match &attr.attr { + let static_attrs = el.attributes.iter().map(|attr| match &attr.attr { ElementAttr::AttrText { name, value } if value.is_static() => { let value = value.source.as_ref().unwrap(); - Some(quote! { + quote! { ::dioxus::core::TemplateAttribute::Static { name: dioxus_elements::#el_name::#name.0, namespace: dioxus_elements::#el_name::#name.1, @@ -192,12 +192,12 @@ impl<'a> DynamicContext<'a> { // todo: we don't diff these so we never apply the volatile flag // volatile: dioxus_elements::#el_name::#name.2, } - }) + } } ElementAttr::CustomAttrText { name, value } if value.is_static() => { let value = value.source.as_ref().unwrap(); - Some(quote! { + quote! { ::dioxus::core::TemplateAttribute::Static { name: dioxus_elements::#el_name::#name.0, namespace: dioxus_elements::#el_name::#name.1, @@ -206,7 +206,7 @@ impl<'a> DynamicContext<'a> { // todo: we don't diff these so we never apply the volatile flag // volatile: dioxus_elements::#el_name::#name.2, } - }) + } } ElementAttr::AttrExpression { .. } @@ -217,7 +217,7 @@ impl<'a> DynamicContext<'a> { let ct = self.dynamic_attributes.len(); self.dynamic_attributes.push(attr); self.attr_paths.push(self.current_path.clone()); - Some(quote! { ::dioxus::core::TemplateAttribute::Dynamic { id: #ct } }) + quote! { ::dioxus::core::TemplateAttribute::Dynamic { id: #ct } } } }); diff --git a/packages/rsx/src/node.rs b/packages/rsx/src/node.rs index 11ec1a0b2..cb604654e 100644 --- a/packages/rsx/src/node.rs +++ b/packages/rsx/src/node.rs @@ -141,7 +141,7 @@ impl ToTokens for BodyNode { pat, expr, body, .. } = exp; - let renderer = TemplateRenderer { roots: &body }; + let renderer = TemplateRenderer { roots: body }; tokens.append_all(quote! { __cx.make_node( diff --git a/packages/ssr/src/lib.rs b/packages/ssr/src/lib.rs index 5dc61d369..46587d6ad 100644 --- a/packages/ssr/src/lib.rs +++ b/packages/ssr/src/lib.rs @@ -7,7 +7,7 @@ pub mod template; use dioxus_core::{Element, LazyNodes, Scope, VirtualDom}; use std::cell::Cell; -use crate::renderer::Renderer; +pub use crate::renderer::Renderer; /// A convenience function to render an `rsx!` call to a string /// @@ -44,3 +44,12 @@ pub fn render_lazy(f: LazyNodes<'_, '_>) -> String { pub fn render(dom: &VirtualDom) -> String { Renderer::new().render(dom) } + +/// A convenience function to pre-render an existing VirtualDom to a string +/// +/// We generally recommend creating a new `Renderer` to take advantage of template caching. +pub fn pre_render(dom: &VirtualDom) -> String { + let mut renderer = Renderer::new(); + renderer.pre_render = true; + renderer.render(dom) +}