-
-
Notifications
You must be signed in to change notification settings - Fork 13
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
The refresh() is not working properly #50
Comments
The Please read the Caching Guide to get an understanding when |
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;
}; Pre v0.18.0import { hash } from "ohash";
export const useCurrentUser = () => {
const nuxt = useNuxtApp();
const data = useAuthData("api/v1/user/@me");
// make refresh ignore the cache
// internal key, could break with a library update
const userKey = `$party${hash([
"auth", // endpointId
"api/v1/user/@me", // path
undefined, // query
undefined, // method
undefined, // body (except formdata)
])}`;
const { refresh } = data;
const refreshInvalidate: typeof refresh = async (opts) => {
// invalidate the cache
delete nuxt.payload.data[userKey];
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 #51. What do you say? |
Environment
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
The text was updated successfully, but these errors were encountered: