-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Data is refetched when using same key in useAsyncData on client side navigation #19027
Comments
I think we can track in #15445 - this was a change before release so data isn't cached by default unless you have enabled payload extraction. |
@danielroe Thank you for the quick reply. Is there another issue on the silent crash when accessing data before await? |
All asyncData errors are silent - you need to check the
edit: just saw you provided a stackblitz - checking that edit: no, I can't reproduce |
Error is
Which makes sense, however I would expect to also get this error when using <template>
<div>
<NuxtLink to="/test1">Go to test1</NuxtLink><br /><br />
<button @click="loadMore">Load more</button><br />
{{ error }}
<ul>
<li v-for="number in data">{{ number }}</li>
</ul>
</div>
</template>
<script setup lang="ts">
const { data, refresh, error } = useAsyncData('test', async () => {
const number = Math.random();
console.log({ data });
await new Promise((r) => setTimeout(r, 10));
console.log({ number });
return [...(data.value ?? []), [number]];
});
const loadMore = () => {
refresh();
};
</script> Changing it to <template>
<div>
<NuxtLink to="/test1">Go to test1</NuxtLink><br /><br />
<button @click="loadMore">Load more</button><br />
{{ error }}
<ul>
<li v-for="number in data">{{ number }}</li>
</ul>
</div>
</template>
<script setup lang="ts">
const { data, refresh, error } = useAsyncData('test', async () => {
const number = Math.random();
await new Promise((r) => setTimeout(r, 10));
console.log({ data });
console.log({ number });
return [...(data.value ?? []), [number]];
});
const loadMore = () => {
refresh();
};
</script> fixes the error |
Environment
Stackblitz
Reproduction
https://stackblitz.com/edit/github-bqbjuq?file=pages%2Ftest1.vue,pages%2Findex.vue
Navigate back and forth between test1 and index and notice new random numbers.
Describe the bug
When using
useAsyncData
with a static key, I would expect data to only be fetched once. However when navigating back and forth,useAsyncData
is called repeatedly.I would expect the data to stay the same when the key is the same.
I have implemented my
useAsyncData
like so:The reason I use the existing data.value is so we can load more data, when a user requests more. In this case by clicking a button. We use this pattern for pagination.
Oddly enough this doesn't happen when nothing is awaited in
useAsyncData
.Another possibly unrelated bug is that the component crashes silently, when data is access before anything is awaited.
So this crashes:
and this doesn't:
Additional context
No response
Logs
No response
The text was updated successfully, but these errors were encountered: