Kong / swrv

Stale-while-revalidate data fetching for Vue

Home Page:https://docs-swrv.netlify.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Feature request] Distinguish between "validating because no data" and "validating because stale"

matthew-dean opened this issue · comments

I just ran into this issue.

I have a use case in which query parameters are updated, and those parameters, because they are Vue refs, force the query to re-validate. The parameters + url make up the key.

Now, the problem is: when I'm sending new parameters that aren't currently cached, I need to show a loading spinner. However, if the results are cached, I want to show the data immediately and NOT show the spinner. The issue I'm running into is that this library does not distinguish those states. It sets a flag of isValidating for the length of the fetch request, even if the data is immediately updated from cache. Because there is no flag between "validating because no data for this key" and "validating because we re-validate on any change in refs", I can't see any (easy) way to set this state.

There is a state management example on the docs site.

I believe you could customize the useSwrvState to accept your additional params (etc.) and then write some logic to differentiate between showing cached data immediately or showing the loader.