leptos-rs / leptos

Build fast web applications with Rust.
https://leptos.dev
MIT License
16.08k stars 633 forks source link

Bug: Unexpected Server-Side Resource Evaluation in Leptos 0.7-beta #2804

Closed nikessel closed 2 months ago

nikessel commented 2 months ago

Description

In the latest Leptos 0.7-beta release, there's an issue with the new resource .await syntax. The resource is making an unexpected request internally on the server, which doesn't occur with the old synchronous .get() method.

Reproduction Steps

  1. Use Leptos 0.7-beta
  2. Create a server function and a resource using the new .await syntax
  3. Observe the behavior in two scenarios: with and without early return on the server

Code Example

pub mod get {
    use super::*;
    #[server(client = Client, input = Cbor, output = Cbor)]
    pub async fn user() -> Result<entity::User, ServerFnError> {
        let user = Faker.fake();
        tracing::debug!("User sent from server: {:?}", user);
        user
    }
}

let user = Resource::new(
    move || is_open.track(),
    #[allow(unreachable_code)]
    |()| async move {
        // Scenario 1
        #[cfg(feature = "ssr")]
        return None;
        // Scenario 2
        // #[cfg(feature = "ssr")]
        // return None;
        match get::user().await {
            Ok(user) => Some({
                tracing::debug!("User received from server: {:?}", user);
                user
            }),
            Err(e) => {
                tracing::error!("Error fetching user: {}", e);
                None
            }
        }
    },
);

view! {
    <Suspense>
        <p>"User ID: " {move || Suspend::new(async move {
            match user.await {
                // ...
            }
        })}</p>
    </Suspense>
}

Observed Behavior

Scenario 1 (with early return on server)

Browser logs:

2024-08-09T09:17:20.464Z DEBUG crate::components::profile: User received from server: User { first_name: "Alivia", last_name: "Medhurst", email: "maximo@example.com", job_title: Some("Lead Legal Director"), company: Some("Kihn and Schuppe and Sons") }

Server logs:

2024-08-09T09:17:20.456495Z DEBUG crate::server::functions: User sent from server: User { first_name: "Alivia", last_name: "Medhurst", email: "maximo@example.com", job_title: Some("Lead Legal Director"), company: Some("Kihn and Schuppe and Sons") }

Scenario 2 (without early return on server)

Browser logs:

2024-08-09T09:19:08.585Z DEBUG app_name::components::profile: User received from server: User { first_name: "Muriel", last_name: "Dickens", email: "leilani@example.org", job_title: None, company: Some("Kris and Jast Inc") }

Server logs:

2024-08-09T09:19:01.346753Z DEBUG app_name::server::functions: User sent from server: User { first_name: "Jarvis", last_name: "Feeney", email: "adalberto@example.com", job_title: Some("Senior IT Officer"), company: Some("Miller Group") }
2024-08-09T09:19:01.346858Z DEBUG app_name::components::profile: User received from server: User { first_name: "Jarvis", last_name: "Feeney", email: "adalberto@example.com", job_title: Some("Senior IT Officer"), company: Some("Miller Group") }
2024-08-09T09:19:08.579178Z DEBUG app_name::server::functions: User sent from server: User { first_name: "Muriel", last_name: "Dickens", email: "leilani@example.org", job_title: None, company: Some("Kris and Jast Inc") }

Expected Behavior

The resource should not make a request internally on the server. This behavior should be consistent with the old synchronous .get() method of managing resources.

nikessel commented 2 months ago

Seems it might be an issue with my code, I'm not seeing the same problem in another resource