diff --git a/leptos_dom/src/render_to_string.rs b/leptos_dom/src/render_to_string.rs index e15e4cc67..72d7b6ecc 100644 --- a/leptos_dom/src/render_to_string.rs +++ b/leptos_dom/src/render_to_string.rs @@ -12,12 +12,28 @@ pub fn escape_attr(text: &str) -> Cow<'_, str> { } cfg_if! { - if #[cfg(feature = "ssr")] { + if #[cfg(any(doc, feature = "ssr"))] { use leptos_reactive::*; use crate::Element; use futures::{stream::FuturesUnordered, Stream, StreamExt}; + /// Renders a component to a static HTML string. + /// + /// ``` + /// # use leptos_reactive::*; use leptos_dom::*; use leptos_macro::view; + /// let html = render_to_string(|cx| view! { cx, + ///
"Hello, world!"
+ /// }); + /// assert_eq!(html, r#"Hello, world!
"#); + /// ``` + pub fn render_to_string(view: impl FnOnce(Scope) -> Element + 'static) -> String { + let runtime = create_runtime(); + let html = run_scope(runtime, move |cx| view(cx)); + runtime.dispose(); + html + } + /// Renders a component to a stream of HTML strings. /// /// This renders: @@ -30,7 +46,7 @@ cfg_if! { /// it is waiting for a resource to resolve from the server, it doesn't run it initially. /// 3) HTML fragments to replace each `