Skip to content

Commit

Permalink
Don't stream down resources not read under a <Suspense/>, fixing re…
Browse files Browse the repository at this point in the history
…ndering issue
  • Loading branch information
gbj committed Dec 23, 2022
1 parent dc8dbf2 commit 2e68f74
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion leptos_dom/src/ssr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
22 changes: 18 additions & 4 deletions leptos_reactive/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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::<SuspenseContext>(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);
Expand Down
13 changes: 13 additions & 0 deletions leptos_reactive/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ResourceId> {
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<PinnedFuture<(ResourceId, String)>> {
Expand Down
6 changes: 6 additions & 0 deletions leptos_reactive/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ResourceId> {
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<PinnedFuture<(ResourceId, String)>> {
with_runtime(self.runtime, |runtime| runtime.serialization_resolvers())
Expand Down

0 comments on commit 2e68f74

Please sign in to comment.