Skip to content

fix(nuxt): ensure newKey asyncData runs when oldKey asyncData is still running #32466

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

Merged
merged 10 commits into from
Jun 26, 2025
4 changes: 3 additions & 1 deletion packages/nuxt/src/app/composables/asyncData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ export function useAsyncData<
const unsubExecute = watch([key, ...(options.watch || [])], ([newKey], [oldKey]) => {
if ((newKey || oldKey) && newKey !== oldKey) {
const hasRun = nuxtApp._asyncData[oldKey]?.data.value !== undefined
const isRunning = nuxtApp._asyncDataPromises[oldKey] !== undefined
Copy link
Preview

Copilot AI Jun 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add unit tests (e.g., using Vitest) for the new key-change logic in useAsyncData to cover cases where isRunning is true, as required by our core functionality testing guidelines.

Copilot uses AI. Check for mistakes.

if (oldKey) {
unregister(oldKey)
}
Expand All @@ -336,7 +337,8 @@ export function useAsyncData<
nuxtApp._asyncData[newKey] = createAsyncData(nuxtApp, newKey, _handler, options, initialFetchOptions.cachedData)
}
nuxtApp._asyncData[newKey]._deps++
if (options.immediate || hasRun) {

if (options.immediate || hasRun || isRunning) {
nuxtApp._asyncData[newKey].execute(initialFetchOptions)
}
} else {
Expand Down
26 changes: 26 additions & 0 deletions test/nuxt/composables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,32 @@ describe('useFetch', () => {
await nextTick()
expect.soft(status.value).toBe('pending')
})

it('should handle parallel execute with `immediate: false`', async () => {
const query = reactive({ q: 1 })
const { execute, status } = useFetch(
'/api/test',
{
query,
immediate: false,
},
)
watch(query, () => execute(), { once: true })

expect.soft(status.value).toBe('idle')
query.q++
query.q++

await nextTick()
query.q++

expect.soft(status.value).toBe('pending')
await vi.waitFor(() => {
expect(status.value).toBe('success')
})
query.q++
expect.soft(status.value).toBe('pending')
})
})

describe('errors', () => {
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy