Comments (8)
Digging into the source code I discovered that I was right. refreshNuxtData
only re-fetches the data when useAsyncData is in scope:
When it goes out of scope, the hook is removed:
I don't think it's a bug at this point, but at the same time it's not documented anywhere. In my opinion it should work even if useAsyncData is not in scope (how can i "invalidate" the cache from another page?). π€
from nuxt.
Hello!
The thing with v-if is that it completely removes the component from the tree and as such vue will not update that component anymore since the component is gone. Now when the component reappears, because vue previously destroys that component, a new instance will need to be created using "initial state", where the "initial state" is the data previously fetched on the server side.
So what can u do to fix it? Well here are some suggestions:
- First is to utilize the v-show attribute instead as it will not remove the component from the tree and only changes the
display
property.- Use the KeepAlive built in component provided by vue so that the component deactivate instead of unmounted.
- Utilize onMounted lifecycle hooks provided by vue to call refresh, although this means it will be triggered on initial render as well.
Hope this helps!
The problem is that refreshNuxtData doesnβt re-fetch the data as stated in the docs:
https://nuxt.com/docs/api/utils/refresh-nuxt-data
from nuxt.
Start a new pull request in StackBlitz Codeflow.
from nuxt.
Oh, i have the same problem
from nuxt.
What's the solution?
from nuxt.
I've got a similar problem. Some times refreshNuxtData
doesn't trigger a fetch. Confirmed by reviewing network tab in browser dev tools.
It's mysterious because:
- I checked that the nuxt payload exists in nuxt devtools, under payload tab.
- And I checked that I am passing in the correct key into
refreshNuxtData
that corresponds to the keyed state I want to refresh.
Presently thinking through the best way to share a reproduction based on my use case.
from nuxt.
Got a work around I can use for now. Which is to do a fully manual refresh for the given key:
const { refresh } = await useFetch('/some/resource', { key: 'unique-key', watch: false, immediate: false })
Then I would replace the call to refreshNuxtData('unique-key')
with a call to refresh()
from nuxt.
I'm also having this problem using a wrapper around useFetch
to use te cached value
export default async <T>(url: string) => {
// Use nuxtData to cache data
const { data: cached } = useNuxtData<T>(url);
if (!cached.value) {
const { data, error } = await useFetch<T>(url, { key: url });
if (error.value) {
throw createError({
...error.value,
statusMessage: `Could not fetch data from ${url}`,
});
}
// Update the cache
cached.value = data.value as T;
} else {
console.log(`Getting value from cache for ${url}`);
}
return cached;
};
The idea is to use my wrapper and refresh the data when needed
const data = useCachedFetch<UserProfile>('/api/profile')
// somewere else after updating the profile
refreshNuxtData('/api/profile')
I can see that the network request is not made when using my wrapper but using useFetch
directly it does.
The same happens if the useFetch
is wrapped in callOnce
from nuxt.
Related Issues (20)
- Tailwindcss added at last is not applied HOT 5
- Disabling SSR prevents remaining CSS from loading HOT 5
- Error: Cannot find module punycode/ HOT 9
- Img @error not working HOT 2
- useHead undefined after initiating a regex (since monday) HOT 4
- keepalive not work HOT 1
- String vs object errors with `createError` and accessing server-thrown errors on client
- Tailwind css issue related to CSS specificity on dev server HOT 1
- keep alive caches are destroyed when visit keep alive excluded page HOT 3
- The package generated under the .output/server/node_modules file is a hyperlink. HOT 2
- 'text/html' is not a valid JavaScript MIME type. HOT 2
- This module cannot be imported in the Vue part of your app. [importing @nuxt/kit from app.vue] causing page to not work HOT 5
- If a component uses empty data in `useFetch`, then `clearError` will be invalidated HOT 2
- Cache control header missing in response headers (while using nitro.publicAssets) HOT 7
- [nuxt] [request error] [unhandled] [500] _fetch is not a function HOT 3
- feat: support passing options to `TypedRouterOptions` when using `experimental.typedPages` HOT 3
- Dynamic layouts HOT 4
- Previous route info HOT 2
- Imported CSS files not correctly injected in circumstances involving lazy-loaded components HOT 1
- Nuxt 3 Dev build time is very slow in WSL 2 Linux Ubuntu ~ ranging up to 30 seconds just to refresh changes HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nuxt.