Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading a resource in an effect incorrectly(?) results in a warning about hydration issues #2480

Open
Silvea12 opened this issue Mar 31, 2024 · 2 comments
Labels
documentation Improvements or additions to documentation

Comments

@Silvea12
Copy link

Silvea12 commented Mar 31, 2024

Describe the bug

When reading a resource in a hydrated context, usually via an effect, you get a hydration warning.

Warning text

At src/app.rs:48:39, you are reading a resource in hydrate mode outside a or . This can cause hydration mismatch errors and loses out on a significant performance optimization. To fix this issue, you can either:

  1. Wrap the place where you read the resource in a or component, or
  2. Switch to using create_local_resource(), which will wait to load the resource until the app is hydrated on the client side. (This will have worse performance in most cases.) leptos-fullstack-test.js:875:9

Leptos Dependencies

Please copy and paste the Leptos dependencies and features from your Cargo.toml.

For example:

leptos = { version = "0.6", features = ["nightly"] }
leptos_axum = { version = "0.6", optional = true }
leptos_meta = { version = "0.6", features = ["nightly"] }
leptos_router = { version = "0.6", features = ["nightly"] }

To Reproduce
Steps to reproduce the behavior:
Create a server fn:

#[server]
async fn get_info() -> Result<String, ServerFnError> {
    Ok("Hello, world!".to_string())
}

Put this in a component:

let e = create_resource(|| (), |_| get_info());
create_effect(move |_| log!("{:?}", e.get()));

The Warning appears on hydration

Expected behavior
No warning should appear

@gbj gbj added the documentation Improvements or additions to documentation label Apr 24, 2024
@gbj
Copy link
Collaborator

gbj commented Apr 24, 2024

The warning here will just need to be updated so that it is disabled in user effects -- you are correct that there is no actual issue, it's a false positive.

@luxalpa
Copy link
Contributor

luxalpa commented Apr 25, 2024

This is a big problem for me too. I get a lot of these messages on app start, and I am very nervous that in some cases it could turn out to be an issue in my code (I'm basically trying to carefully only use the with and get after the resource has already been loaded, but there's no guarantee that I don't accidentally put it in the wrong place).

Is there a workaround for this?

Other than writing provide_context(SuspenseContext::new()); // TODO: Remove?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants