From 9fd298744750901b61de9a5801e1978a194a8884 Mon Sep 17 00:00:00 2001 From: Greg Johnston Date: Sat, 2 Mar 2024 11:57:35 -0500 Subject: [PATCH] fix: correctly reset hydration status in islands mode Suspense (closes #2332) (#2393) --- leptos/src/suspense_component.rs | 30 ++++++++++++++++++++++++++---- leptos_reactive/src/hydration.rs | 2 ++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/leptos/src/suspense_component.rs b/leptos/src/suspense_component.rs index b081576579..bf99d97c7d 100644 --- a/leptos/src/suspense_component.rs +++ b/leptos/src/suspense_component.rs @@ -173,6 +173,9 @@ where runtime, ); + #[cfg(feature = "experimental-islands")] + let prev_no_hydrate = + SharedContext::no_hydrate(); #[cfg(feature = "experimental-islands")] { SharedContext::set_no_hydrate( @@ -180,7 +183,7 @@ where ); } - with_owner(owner, { + let rendered = with_owner(owner, { move || { HydrationCtx::continue_from( current_id, @@ -194,7 +197,15 @@ where .render_to_string() .to_string() } - }) + }); + + #[cfg(feature = "experimental-islands")] + SharedContext::set_no_hydrate( + prev_no_hydrate, + ); + + #[allow(clippy::let_and_return)] + rendered } }, // in-order streaming @@ -205,6 +216,9 @@ where runtime, ); + #[cfg(feature = "experimental-islands")] + let prev_no_hydrate = + SharedContext::no_hydrate(); #[cfg(feature = "experimental-islands")] { SharedContext::set_no_hydrate( @@ -212,7 +226,7 @@ where ); } - with_owner(owner, { + let rendered = with_owner(owner, { move || { HydrationCtx::continue_from( current_id, @@ -225,7 +239,15 @@ where .into_view() .into_stream_chunks() } - }) + }); + + #[cfg(feature = "experimental-islands")] + SharedContext::set_no_hydrate( + prev_no_hydrate, + ); + + #[allow(clippy::let_and_return)] + rendered } }, ); diff --git a/leptos_reactive/src/hydration.rs b/leptos_reactive/src/hydration.rs index c2166787ac..d785af8748 100644 --- a/leptos_reactive/src/hydration.rs +++ b/leptos_reactive/src/hydration.rs @@ -339,11 +339,13 @@ thread_local! { impl SharedContext { /// Whether the renderer should currently add hydration IDs. pub fn no_hydrate() -> bool { + println!("no_hydrate == {}", NO_HYDRATE.with(Cell::get)); NO_HYDRATE.with(Cell::get) } /// Sets whether the renderer should not add hydration IDs. pub fn set_no_hydrate(hydrate: bool) { + println!("set_no_hydrate == {}", hydrate); NO_HYDRATE.with(|cell| cell.set(hydrate)); }