fix suspense

This commit is contained in:
Evan Almloff committed 2023-07-17 16:48:54 -07:00
1 parent 913d1f0491
commit dd5d974aeb
12 files changed
+351 -110

No files matched your search

@@ -0,0 +1,34 @@
use serde::{de::DeserializeOwned, Serialize};
/// This allows you to send data from the server to the client. The data is serialized into the HTML on the server and hydrated on the client.
///
/// When you run this function on the client, you need to be careful to insure the order you run it initially is the same order you run it on the server.
///
/// If Dioxus fullstack cannot find the data on the client, it will run the closure again to get the data.
///
/// # Example
/// ```rust
/// use dioxus::prelude::*;
/// use dioxus_fullstack::prelude::*;
///
/// fn app(cx: Scope) -> Element {
/// let state1 = use_state(cx, || from_server(|| {
/// 1234
/// }));
/// }
/// ```
pub fn server_cached<O: 'static + Serialize + DeserializeOwned>(server_fn: impl Fn() -> O) -> O {
#[cfg(feature = "ssr")]
{
let data = server_fn();
let sc = crate::prelude::server_context();
if let Err(err) = sc.push_html_data(&data) {
log::error!("Failed to push HTML data: {}", err);
}
data
}
#[cfg(not(feature = "ssr"))]
{
crate::html_storage::deserialize::take_server_data().unwrap_or_else(|| server_fn())
}
}