diff --git a/leptos_server/Cargo.toml b/leptos_server/Cargo.toml index 146f0a126..98ebce5b8 100644 --- a/leptos_server/Cargo.toml +++ b/leptos_server/Cargo.toml @@ -18,6 +18,7 @@ server_fn = { workspace = true } tracing = { version = "0.1", optional = true } futures = "0.3" +any_spawner = { workspace = true } tachys = { workspace = true, optional = true, features = ["reactive_graph"] } # serialization formats diff --git a/leptos_server/src/local_resource.rs b/leptos_server/src/local_resource.rs index b91f8a7ba..30e2e5b58 100644 --- a/leptos_server/src/local_resource.rs +++ b/leptos_server/src/local_resource.rs @@ -48,6 +48,12 @@ impl ArcLocalResource { if cfg!(feature = "ssr") { pending().await } else { + // LocalResources that are immediately available can cause a hydration error, + // because the future *looks* like it is alredy ready (and therefore would + // already have been rendered to html on the server), but in fact was ignored + // on the server. the simplest way to avoid this is to ensure that we always + // wait a tick before resolving any value for a localresource. + any_spawner::Executor::tick().await; fut.await } } @@ -198,6 +204,12 @@ impl LocalResource { if cfg!(feature = "ssr") { pending().await } else { + // LocalResources that are immediately available can cause a hydration error, + // because the future *looks* like it is alredy ready (and therefore would + // already have been rendered to html on the server), but in fact was ignored + // on the server. the simplest way to avoid this is to ensure that we always + // wait a tick before resolving any value for a localresource. + any_spawner::Executor::tick().await; fut.await } }