Skip to content

isValidating is still true when using useSWR just after mutate() #1407

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

Closed
hazae41 opened this issue Aug 29, 2021 · 4 comments · Fixed by #1411
Closed

isValidating is still true when using useSWR just after mutate() #1407

hazae41 opened this issue Aug 29, 2021 · 4 comments · Fixed by #1411
Labels
bug Something isn't working

Comments

@hazae41
Copy link

hazae41 commented Aug 29, 2021

Bug report

Description / Observed Behavior

I use some prefetching technique with mutate() when the user hovers a link

export function HomePage(){
  async function prefetch(){
     await mutate("/api/data", async (cache?: Data) => {
        if (cache) return cache
        return await fetcher<Data>("/api/data")
      }, false)
  }
  
  return <>
    <div 
      onMouseEnter={prefetch}
      onTouchStart={prefetch}>
      <Link href="/data">
        ...
      </Link>
    </div>
  </>
}

export function DataPage(){
  const res = useSWR<Data>("/api/data")

  console.log(res.isValidating) // true

  return <>
    ...
  </>
}

However, when the user clicks too fast, mutate() starts fetching but on the next page isValidating is still true (and never becomes false).

Expected Behavior

I expect isValidating to be false

Additional Context

[email protected]

@hazae41
Copy link
Author

hazae41 commented Aug 29, 2021

I mean, it's expected to be true for a short moment, but it keeps being true

@hazae41
Copy link
Author

hazae41 commented Aug 29, 2021

This doesn't work either:

await mutate(url, (cache?: T) => {
  if (cache) return cache

  fetcher(url)
    .then(data => mutate(url, data, false))
    .catch(() => { })
}, false)

@hazae41
Copy link
Author

hazae41 commented Aug 29, 2021

After a while it stops and throws a timeout error

@promer94
Copy link
Collaborator

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.

3 participants