dioxus_fullstack/hooks/
server_future.rs

1use dioxus_lib::prelude::*;
2use serde::{de::DeserializeOwned, Serialize};
3use std::future::Future;
4
5/// Runs a future with a manual list of dependencies and returns a resource with the result if the future is finished or a suspended error if it is still running.
6///
7///
8/// On the server, this will wait until the future is resolved before continuing to render. When the future is resolved, the result will be serialized into the page and hydrated on the client without rerunning the future.
9///
10///
11/// <div class="warning">
12///
13/// Unlike [`use_resource`] dependencies are only tracked inside the function that spawns the async block, not the async block itself.
14///
15/// ```rust, no_run
16/// # use dioxus::prelude::*;
17/// // ❌ The future inside of use_server_future is not reactive
18/// let id = use_signal(|| 0);
19/// use_server_future(move || {
20///     async move {
21///          // But the future is not reactive which means that the future will not subscribe to any reads here
22///          println!("{id}");
23///     }
24/// });
25/// // ✅ The closure that creates the future for use_server_future is reactive
26/// let id = use_signal(|| 0);
27/// use_server_future(move || {
28///     // The closure itself is reactive which means the future will subscribe to any signals you read here
29///     let cloned_id = id();
30///     async move {
31///          // But the future is not reactive which means that the future will not subscribe to any reads here
32///          println!("{cloned_id}");
33///     }
34/// });
35/// ```
36///
37/// </div>
38///
39/// # Example
40///
41/// ```rust, no_run
42/// # use dioxus::prelude::*;
43/// # async fn fetch_article(id: u32) -> String { unimplemented!() }
44/// use dioxus::prelude::*;
45///
46/// fn App() -> Element {
47///     let mut article_id = use_signal(|| 0);
48///     // `use_server_future` will spawn a task that runs on the server and serializes the result to send to the client.
49///     // The future will rerun any time the
50///     // Since we bubble up the suspense with `?`, the server will wait for the future to resolve before rendering
51///     let article = use_server_future(move || fetch_article(article_id()))?;
52///
53///     rsx! {
54///         "{article().unwrap()}"
55///     }
56/// }
57/// ```
58#[must_use = "Consider using `cx.spawn` to run a future without reading its value"]
59#[track_caller]
60pub fn use_server_future<T, F>(
61    mut future: impl FnMut() -> F + 'static,
62) -> Result<Resource<T>, RenderError>
63where
64    T: Serialize + DeserializeOwned + 'static,
65    F: Future<Output = T> + 'static,
66{
67    #[cfg(feature = "server")]
68    let serialize_context = crate::html_storage::use_serialize_context();
69
70    // We always create a storage entry, even if the data isn't ready yet to make it possible to deserialize pending server futures on the client
71    #[cfg(feature = "server")]
72    let server_storage_entry = use_hook(|| serialize_context.create_entry());
73
74    #[cfg(feature = "server")]
75    let caller = std::panic::Location::caller();
76
77    // If this is the first run and we are on the web client, the data might be cached
78    #[cfg(feature = "web")]
79    let initial_web_result = use_hook(|| {
80        std::rc::Rc::new(std::cell::RefCell::new(Some(
81            dioxus_web::take_server_data::<T>(),
82        )))
83    });
84
85    let resource = use_resource(move || {
86        #[cfg(feature = "server")]
87        let serialize_context = serialize_context.clone();
88
89        let user_fut = future();
90
91        #[cfg(feature = "web")]
92        let initial_web_result = initial_web_result.clone();
93
94        #[allow(clippy::let_and_return)]
95        async move {
96            // If this is the first run and we are on the web client, the data might be cached
97            #[cfg(feature = "web")]
98            match initial_web_result.take() {
99                // The data was deserialized successfully from the server
100                Some(Ok(Some(o))) => return o,
101
102                // The data is still pending from the server. Don't try to resolve it on the client
103                Some(Ok(None)) => std::future::pending::<()>().await,
104
105                // The data was not available on the server, rerun the future
106                Some(Err(_)) => {}
107
108                // This isn't the first run, so we don't need do anything
109                None => {}
110            }
111
112            // Otherwise just run the future itself
113            let out = user_fut.await;
114
115            // If this is the first run and we are on the server, cache the data in the slot we reserved for it
116            #[cfg(feature = "server")]
117            serialize_context.insert(server_storage_entry, &out, caller);
118
119            out
120        }
121    });
122
123    // On the first run, force this task to be polled right away in case its value is ready
124    use_hook(|| {
125        let _ = resource.task().poll_now();
126    });
127
128    // Suspend if the value isn't ready
129    if resource.state().cloned() == UseResourceState::Pending {
130        let task = resource.task();
131        if !task.paused() {
132            return Err(suspend(task).unwrap_err());
133        }
134    }
135
136    Ok(resource)
137}