Comments (4)
Same applies to useAsyncData
.
It looks like everything that is fetched with these composables is always stored in the NuxtData cache.
I guess there might be use cases when storing old value of data
is desired, like showing progressbar on top of list results.
One way is to use v-else
in template.
If it's important to ensure that the data is getting unset you could clear the cache manually.
const { data, status, clear } = await useFetch(
"https://reqres.in/api/users/?delay=1",
{
lazy: true,
server: false,
},
);
onUnmounted(() => {
clear();
});
When clearing data this way there is an edge case if you navigate to the same page, for example from /post/1
to /post/2
. Calling clear on unmount would cancel the fetch for new page and status
of query would be set to idle
.
To make it work ensure that you pass unique key to useFetch
.
const { data, status, clear } = await useFetch(
"https://reqres.in/api/users/?delay=1",
{
key: `post-${route.params.id}`
lazy: true,
server: false,
},
);
onUnmounted(() => {
clear();
});
clearNuxtData
composable also could be used for clearing the cache.
from nuxt.
I have the same issue but with the error
property, it also gets cached this way.
Here is the reproduction link: https://stackblitz.com/edit/github-6epge4?file=pages%2Ffetch-page.vue
If navigate to Home and then back to Fetch page the old error
is still available.
There are different workarounds for this but I find it not very comfortable in terms of error handling for lazy
data-fetching.
For example we want to redirect to 404 on both - client and server - with useLazyFetch
.
If we don't clear the nuxtData manually on unmount - we might have the following scenario:
1 - user visits /post/123invalid
and gets 404 page
2 - on 404 page user clicks "Go to home page"
3 - from the Home page user navigates to /post/123
If error is not handled correctly - the user could be immediately redirected to error page because the old error
is not null
.
If we want to use the same handler for client and server - possible solution is:
const { error } = useLazyFetch('/api/get-something');
watch(
error,
() => {
if (!error) {
return;
}
if (error.statusCode === 404) {
createError({ status: 404 });
}
},
{
immediate: process.server,
}
);
In the above example it's important to set immediate flag on watcher only on server. So on client it wouldn't trigger during component setup.
But if we decide to change useFetch
to be not lazy
, then it is important that immediate
option on watcher is always true
. Or just not use watcher, check for error synchronously.
I guess it would be more predictable if error
would always get unset when new api call is made, right before the fetch.
Or at least to have some option to configure whether we want to unset error/data
right before the fetch or useAsyncData
handler.
Or maybe have option to configurate automatic cleanup of NuxtData when this data is not used on the page.
from nuxt.
I guess it would be more predictable if
error
would always get unset when new api call is made, right before the fetch.
Didn't think about use cases involving error yet; sounds to me this suggestion makes a lot of sense. Thinking about it, I'm surprised even the error is cached in payload– surely there's a reason for this.
How about a payload: false
option, which then only keeps data, error, status etc. locally?
from nuxt.
Same issue here when using useLazyFetch
with watch
immediate: true
, and currently use onUnmounted
clear()
to solve it.
https://codesandbox.io/p/devbox/uselazyfetch-data-cache-5fnjq8
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.