From 2e68f746dcbf7d9f96397a0c6b8f5107bbf4c5e2 Mon Sep 17 00:00:00 2001 From: Greg Johnston Date: Fri, 23 Dec 2022 16:57:55 -0500 Subject: [PATCH] Don't stream down resources not read under a ``, fixing rendering issue --- leptos_dom/src/ssr.rs | 2 +- leptos_reactive/src/resource.rs | 22 ++++++++++++++++++---- leptos_reactive/src/runtime.rs | 13 +++++++++++++ leptos_reactive/src/scope.rs | 6 ++++++ 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/leptos_dom/src/ssr.rs b/leptos_dom/src/ssr.rs index d9a02e7787..c4c5b18bfc 100644 --- a/leptos_dom/src/ssr.rs +++ b/leptos_dom/src/ssr.rs @@ -116,7 +116,7 @@ pub fn render_to_stream_with_prefix_undisposed( // this does NOT contain any of the data being loaded asynchronously in resources let shell = view(cx).render_to_string(cx); - let resources = cx.all_resources(); + let resources = cx.pending_resources(); let pending_resources = serde_json::to_string(&resources).unwrap(); let prefix = prefix(cx); diff --git a/leptos_reactive/src/resource.rs b/leptos_reactive/src/resource.rs index d9a56a93cb..a6c236cee8 100644 --- a/leptos_reactive/src/resource.rs +++ b/leptos_reactive/src/resource.rs @@ -8,7 +8,7 @@ use std::{ pin::Pin, rc::Rc, }; - +use cfg_if::cfg_if; use crate::{ create_effect, create_isomorphic_effect, create_memo, create_signal, queue_microtask, runtime::{with_runtime, RuntimeId}, @@ -116,9 +116,23 @@ where suspense_contexts: Default::default(), }); - let id = with_runtime(cx.runtime, |runtime| { - runtime.create_serializable_resource(Rc::clone(&r)) - }); + cfg_if! { + if #[cfg(any(feature = "csr", feature = "hydrate"))] { + let id = with_runtime(cx.runtime, |runtime| { + runtime.create_serializable_resource(Rc::clone(&r)) + }); + } else { + let id = if use_context::(cx).is_some() { + with_runtime(cx.runtime, |runtime| { + runtime.create_serializable_resource(Rc::clone(&r)) + }) + } else { + with_runtime(cx.runtime, |runtime| { + runtime.create_unserializable_resource(Rc::clone(&r)) + }) + }; + } + } create_isomorphic_effect(cx, { let r = Rc::clone(&r); diff --git a/leptos_reactive/src/runtime.rs b/leptos_reactive/src/runtime.rs index 3529c5bac0..f37bca08d4 100644 --- a/leptos_reactive/src/runtime.rs +++ b/leptos_reactive/src/runtime.rs @@ -296,6 +296,19 @@ impl Runtime { .collect() } + /// Returns IDs for all [Resource]s found on any scope, pending from the server. + pub(crate) fn pending_resources(&self) -> Vec { + self.resources + .borrow() + .iter() + .filter_map(|(resource_id, res)| if matches!(res, AnyResource::Serializable(_)) { + Some(resource_id) + } else { + None + }) + .collect() + } + pub(crate) fn serialization_resolvers( &self, ) -> FuturesUnordered> { diff --git a/leptos_reactive/src/scope.rs b/leptos_reactive/src/scope.rs index 30d72e4c40..115829cd4d 100644 --- a/leptos_reactive/src/scope.rs +++ b/leptos_reactive/src/scope.rs @@ -282,6 +282,12 @@ impl Scope { with_runtime(self.runtime, |runtime| runtime.all_resources()) } + /// Returns IDs for all [Resource](crate::Resource)s found on any scope that are + /// pending from the server. + pub fn pending_resources(&self) -> Vec { + with_runtime(self.runtime, |runtime| runtime.pending_resources()) + } + /// Returns IDs for all [Resource](crate::Resource)s found on any scope. pub fn serialization_resolvers(&self) -> FuturesUnordered> { with_runtime(self.runtime, |runtime| runtime.serialization_resolvers())