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

Bug: Suspense Data undefined Race Condition #57

Closed
jcarroll2007 opened this issue Nov 1, 2019 · 2 comments · Fixed by #82
Closed

Bug: Suspense Data undefined Race Condition #57

jcarroll2007 opened this issue Nov 1, 2019 · 2 comments · Fixed by #82
Assignees
Labels
bug Something isn't working

Comments

@jcarroll2007
Copy link

I'm not sure if this is a bug, or if this behavior is intended, but I was running into an issue with the line of code below.

So, I'm using useSWR(/api/users/${id}, fetch, {suspense: true}) where the key is dynamic based on the id of the object I want to get (in this contrived example the user's id).

My code looks something like this exampled:

function useUser(id) {
  const user = useSWR(`/api/users/${id}`, fetch, {suspense: true})
  console.log(user)
  return user
}

When this hook is called multiple times with different IDs, the logs would look like this:

// render 1 with user id 1
{ id:1 }
// render 2 with user id 2
undefined
// render 3
{ id: 2 }

The reason behind this is the two LOC referenced below.

So, my question is, in render 2 on L342 typeof data = Object, not undefined because the data is reset on line 358 (afterwards). Should there be a line before line 342 that checks to see if the keyRef.current and the new key are then different and reset data there so that the revalidate can happen immediately?

I quickly implemented this locally and this fixed my issue and prevented render 2 happening where the data was undefined.

Thanks! Loving this library so far.

https://github.com/zeit/swr/blob/9b51b7d066335d7249d114e2b9a2cb5d633795b6/src/use-swr.ts#L342
https://github.com/zeit/swr/blob/9b51b7d066335d7249d114e2b9a2cb5d633795b6/src/use-swr.ts#L358

@shuding
Copy link
Member

shuding commented Nov 1, 2019

This is indeed a good optimization. Thank you @jcarroll2007 so much!

@shuding shuding self-assigned this Nov 1, 2019
@shuding shuding added the bug Something isn't working label Nov 1, 2019
@jcarroll2007
Copy link
Author

Awesome! Thanks for responding so quickly! Really excited about this hook.

This was referenced Nov 3, 2019
shuding added a commit that referenced this issue Nov 6, 2019
pacocoursey added a commit that referenced this issue Nov 6, 2019
* fix suspense on hydration

* refactor the code; add error to the cache

* fix suspense and error catching

* add test case for #57

* Update src/use-swr.ts

* Apply suggestions from code review

Co-Authored-By: Paco <34928425+pacocoursey@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants