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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Re-use getCachedData
when refresh
ing asyncData
#24332
Comments
in my opinion, it should be default behavior that all requests, even those made by reactive changes, use the cache if your definition of i.e. the definition of i understand that you're thinking about potential implications for folks that have already implemented this feature, but introducing a |
I think that sounds fair, if the function is specified and returns data, it should be used instead of calling again by default. But it'd be still nice to have a chance to bypass the function "on demand" (which would be equivalent to a hard reload or similar). I wonder if we need the |
ah, yes. maybe it would be best if reactive changes attempt to use the cache, but or, like you suggested, |
Hi! The problem is that const { data } = await useFetch('https://icanhazdadjoke.com/', {
key: 'myOwnKey',
query: { page },
getCachedData: (key) => nuxt.payload.data[key] || nuxt.static.data[key],
headers: {
Accept: 'application/json',
},
}); Hope this helps! 馃憢 |
I thought so too initially but it is not enough if e.g. |
Do you mean like in this example? |
As soon as you use a page parameter it works, but if you "re-use" a local |
Thanks, I get it now 馃檪 There's already an option in asyncData - if ((opts._initial || (nuxt.isHydrating && opts._initial !== false)) && hasCachedData()) {
return Promise.resolve(options.getCachedData!(key))
} So this could be the way to "re-use" a local <button @click="refresh({ _initial: true })">New Joke (refresh)</button> Maybe it's a bit "hacky" but it should work 馃 |
@darioferderber |
Yup, I know, but it could help @broox if he's still struggling with this issue 馃槃 I agree, there should be a better DX to deal with this, but basically it should work the same as |
@darioferderber - i don't think the private the http request is automatically triggered when i change the reactive |
Internally, a |
Is there any update on this one? I spent quite a while thinking it was my own bug trying figuring out why my getCachedData function was only being triggered on the initial requests and not re-used when my watch sources changes. This would be a great addition and could save me a bunch of request with my paginated data. |
Describe the feature
Hey 馃憢馃徎
With the introduction of
getCachedData
,useFetch
anduseAsyncData
have an elegant way to re-use existing data easily.There is a downside though, as @broox mentioned in this YouTube comment:
When refreshing the data, either via calling
refresh
manually or implicitly using it through thewatch
property or by defining reactive parameters likequery
in theuseFetch
options, there is no option to re-use the cached data.Reason behind it is that we only take cached data into account when nuxt is hydrating or on the initial request of the composable.
nuxt/packages/nuxt/src/app/composables/asyncData.ts
Lines 182 to 184 in a087b87
Spontaneous idea to resolve this issue:
force
parameter forrefresh
, so manualrefresh()
calls can bypass thegetCachedData
function if necessary. By default, this can be set totrue
if we want to avoid breaking changes.force
tofalse
by default forwatch
. This is a behavior change, but when implementing an owngetCachedData
function you can take this into account easily in there. Projects not using thegetCachedData
function should not be impacted.More than happy for other ideas or feedback鈽猴笍
Additional information
Final checks
The text was updated successfully, but these errors were encountered: