Only stream Resources if they're under a Suspense to fix rendering issue

This commit is contained in:
Greg Johnston 2022-12-23 17:01:22 -05:00
parent af68da0a9a
commit a4747596fa
4 changed files with 38 additions and 5 deletions

View file

@ -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);

View file

@ -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::<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);

View file

@ -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)>> {

View file

@ -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())