Closed AQian-Cup closed 1 year ago
The useMyApiData
composable caches the responses just like earlier versions of useFetch
did by hashing the input parameters.
Please read the Caching Guide to get an understanding when useMyApiData
caches data and how to use the cache: false
option.
In case you want more control over caching, I made a composable for one of my endpoints that makes refresh invalidate the cache. It replicates some internal logic to calculate the key, so it might break in the future if nuxt-api-party or ohash updates.
For v0.20.0
export const useCurrentUser = () => {
const nuxt = useNuxtApp();
const dataKey = "dataKey"
const data = useAuthData("api/v1/user/@me", {
key: dataKey
});
// make refresh ignore the cache
const { refresh } = data;
const refreshInvalidate: typeof refresh = async (opts) => {
// invalidate the cache
delete nuxt.payload.data[`$apiParty${dataKey}`];
await refresh(opts);
};
data.refresh = refreshInvalidate;
// awaiting gets the original asyncdata object, which is separate from the Promise
data.then((asyncData) => {
asyncData.refresh = refreshInvalidate;
});
return data;
};
@killjoy1221 Thanks for your example! It also brought an idea to mind. See https://github.com/johannschopplich/nuxt-api-party/pull/51. What do you say?
Environment
Build Modules: -
Reproduction
https://codesandbox.io/p/sandbox/happy-rubin-r396mz
Describe the bug
When I use refresh(), the data doesn't get updated, and even the network request doesn't happen. So I used Nuxt3's native useFetch(), which can send network requests even though it doesn't fetch the data properly (because no proxy is configured).
Is this nuxt-api-party's fault? Or does it need to be written differently to achieve this effect?
I'm sorry if this problem bothers you.
Additional context
No response
Logs
No response