N
Nuxt3w ago
Numil

How to have the same behavior for fetching data between F5/navigator refresh and navigation

Hello, i want to block the loading/navigation even when using navigateTo from one page to the next, the same as when having a navigator reload. i use useAsyncData in my page and it waits for the whole request when accessing the page, but not when navigating in between 2 pages, i want to wait in both cases
5 Replies
kapa.ai
kapa.ai3w ago
Hi @Numil. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To achieve the same behavior for data fetching between page refreshes (F5) and navigation between pages, you need to understand how Nuxt's data fetching works with navigation. By default, useAsyncData will block navigation until the data is fetched when you first load a page (F5/refresh). However, when navigating between pages using <NuxtLink> or navigateTo(), Nuxt uses a different behavior with the lazy option. To ensure that navigation is blocked until data is fetched in both scenarios, you need to make sure you're not using the lazy option in your useAsyncData calls. Here's how to achieve consistent behavior:
// This will block navigation until data is fetched
const { data } = await useAsyncData('key', () => {
return $fetch('/api/data')
})
// This will block navigation until data is fetched
const { data } = await useAsyncData('key', () => {
return $fetch('/api/data')
})
The key points to understand: 1. By default, useAsyncData and useFetch will wait for the resolution of their asynchronous function before navigating to a new page by using Vue's Suspense Data Fetching - Lazy. 2. If you're experiencing different behavior between page refresh and navigation, check if you're using: - useLazyAsyncData or useLazyFetch which automatically set lazy: true - Explicitly setting lazy: true in your options 3. Make sure you're properly awaiting the data fetching composable in your component setup. If you're still experiencing issues, you might need to check: - Are you using the same data fetching approach consistently across all pages? - Are there any route-specific configurations that might be affecting the behavior? - Is the data fetching happening at the page component level (recommended for this use case)? The status property returned by useAsyncData can help you debug the state of your data fetching:
const { status, data } = await useAsyncData('key', () => {
return $fetch('/api/data')
})

// status can be 'idle', 'pending', 'success', or 'error'
const { status, data } = await useAsyncData('key', () => {
return $fetch('/api/data')
})

// status can be 'idle', 'pending', 'success', or 'error'
This will ensure that navigation is blocked until your data is fetched, providing a consistent experience between page refreshes and in-app navigation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3w ago
<@108292831583440896> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Numil
NumilOP3w ago
@kapa.ai it doesnt resolve my issue, i want the same behavior as when i F5, not the other way around
kapa.ai
kapa.ai3w ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- I understand you want to achieve the same blocking behavior during page navigation as you get with an F5 refresh, where the page waits for data to be fully loaded before rendering. Based on the knowledge sources, there are a few approaches to solve this issue: Option 1: Use a key parameter with useFetch When navigating between pages, useFetch tends to cache data and won't re-fetch when it thinks it's the same API call. Adding a unique key value can force a fresh fetch:
const { data: res } = await useFetch(
"<https://your-api-endpoint.com/",>
{
method: "GET",
key: Date.now(), // Force a new fetch on each navigation
// other options...
}
);
const { data: res } = await useFetch(
"<https://your-api-endpoint.com/",>
{
method: "GET",
key: Date.now(), // Force a new fetch on each navigation
// other options...
}
);
This approach was mentioned as a solution to a similar issue where old data was showing after page navigation Stack Overflow Post. Option 2: Ensure proper async/await structure The way you structure your async code matters. As seen in one of the examples, wrapping your fetch call properly can help:
await useAsyncData(async () => {
if (condition) {
await fetchData();
}
});
await useAsyncData(async () => {
if (condition) {
await fetchData();
}
});
Instead of:
if (condition) {
await useAsyncData(async () => {
fetchData();
});
}
if (condition) {
await useAsyncData(async () => {
fetchData();
});
}
This structure change helped solve a similar issue where requests weren't being properly handled during navigation Stack Overflow Post. Option 3: Check for data existence and handle navigation If the above options don't work, you can explicitly check if data exists and handle navigation accordingly:
const { data, error } = await useAsyncData('key', () => {
return yourService.getData()
});
if (!data.value) {
// Handle the case where data isn't available
// This could include showing a loading state or redirecting
}
const { data, error } = await useAsyncData('key', () => {
return yourService.getData()
});
if (!data.value) {
// Handle the case where data isn't available
// This could include showing a loading state or redirecting
}
This approach ensures that you're properly handling cases where data might not be available during navigation Stack Overflow Post. Remember that Nuxt uses Vue's <Suspense> component under the hood to prevent navigation before async data is available, but the behavior can sometimes be different between a full page refresh and client-side navigation. The approaches above should help you achieve more consistent behavior. Let me know if these solutions work for your specific use case. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3w ago
<@108292831583440896> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?