Kong / swrv

Stale-while-revalidate data fetching for Vue
https://docs-swrv.netlify.app
Apache License 2.0
2.08k stars 72 forks source link

Race condition causes old "useSWRV" to set the isValidating of the new request (/ key). #345

Open shaylevi2 opened 1 year ago

shaylevi2 commented 1 year ago

I believe this is related to https://github.com/Kong/swrv/issues/24

And is being caused by this line: https://github.com/Kong/swrv/blob/master/src/use-swrv.ts#L280

I would create a PR of removing this line, but the code is a bit complex and I'm not sure what else is influenced.

adamdehaven commented 1 year ago

I'm not sure I fully understand the issue you're reporting. Would you mind providing reproduction examples or steps and more details?

shaylevi2 commented 1 year ago

SomeComponent.vue has a prop called id and has the following code in it: const { data, error, isValidating } = useSWRV(() => '/api?id=' + props.id)

Now, if you change the id prop value passed to SomeComponent.vue while a fetch is active, it will trigger another fetch request (as expected) the problem is the isValidating of that new request will become false when the previous fetch call completes when it should obviously wait for the new call to complete (as these are different keys).

If you look at https://github.com/Kong/swrv/pull/26 you'll notice they've added a check to see they are not updating the "IsValidating" of a previous call, but they haven't done so in the problematic line I was referring to: https://github.com/Kong/swrv/blob/master/src/use-swrv.ts#L280

shaylevi2 commented 1 year ago

Did you get a chance to look into it? @adamdehaven

adamdehaven commented 1 year ago

I think this could be resolved by utilizing dependent caching to invalidate the cache when the prop value changes.