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

Default fetcher breaks retrieving data from cache #164

Closed
tochoromero opened this issue Apr 19, 2021 · 3 comments
Closed

Default fetcher breaks retrieving data from cache #164

tochoromero opened this issue Apr 19, 2021 · 3 comments

Comments

@tochoromero
Copy link
Contributor

A common use case we have in our applications is to have a top level component call useSWRV with a fetcher, but then have many lower level component call useSWRV with the same key but without providing a fetcher that way the children components can get the data without forcing to be fetched again, it is only the top level component that is concerned with revalidating the data.

This was working just fine before, but the introduction of a default fetcher changed that behavior, specifically this:
a907d49#diff-0988ac9996eafadcefcd62192f6fa8e63983c4d7b5d04c1604bb21d8096ef77cR185-R189

The documentation still talks about not passing a fetcher to force SWRV to use the cached values, but I don't think that is true anymore with the introduction of a default fetcher. I think having to opt in for the default fetcher is the way to go, maybe if you pass true as the second parameter it will use the default fetcher and if you pass undefined it will use no fetcher and it will fetch from the cache.

@darrenjennings
Copy link
Contributor

@tochoromero re: #148 (comment) I have just updated the docs so hopefully they are more obvious now to help out with this confusion. Thanks for flagging!

@tochoromero
Copy link
Contributor Author

Thank you @darrenjennings. But I don't think the behavior is quite right. Specifically here:
https://github.com/Kong/swrv/blob/master/src/use-swrv.ts#L238

const fetcher = data || fn

If data (the fetcher function) is null, as the documentation suggest, that expression will fallback to fn (the fetch api fetcher).

Instead, that line should probably be something like this:

const fetcher = data ?? (data === undefined ? fn : null)

@darrenjennings
Copy link
Contributor

data (the fetcher function) is null, as the documentation suggest, that expression will fallback to fn (the fetch api fetcher).

It's the reverse, docs say:

If null, swrv will fetch from cache only and not revalidate. If omitted (i.e. undefined) then the fetch api will be used.

@tochoromero here is an example codesandbox, AFAICT it is working as expected https://codesandbox.io/s/swrv-157-79ek4 maybe there is something I am missing though.

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

No branches or pull requests

2 participants