mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
fix(nuxt): await plugin asyncdata promises in nuxt hook (#9616)
This commit is contained in:
parent
ac61b2b69a
commit
4641e8e504
@ -187,7 +187,11 @@ export function useAsyncData<
|
||||
// Server side
|
||||
if (process.server && fetchOnServer && options.immediate) {
|
||||
const promise = initialFetch()
|
||||
onServerPrefetch(() => promise)
|
||||
if (getCurrentInstance()) {
|
||||
onServerPrefetch(() => promise)
|
||||
} else {
|
||||
nuxt.hook('app:created', () => promise)
|
||||
}
|
||||
}
|
||||
|
||||
// Client side
|
||||
|
@ -381,6 +381,7 @@ describe('plugins', () => {
|
||||
it('async plugin', async () => {
|
||||
const html = await $fetch('/plugins')
|
||||
expect(html).toContain('asyncPlugin: Async plugin works! 123')
|
||||
expect(html).toContain('useFetch works!')
|
||||
})
|
||||
})
|
||||
|
||||
@ -885,7 +886,7 @@ describe.skipIf(process.env.NUXT_TEST_DEV || isWindows)('payload rendering', ()
|
||||
it('renders a payload', async () => {
|
||||
const payload = await $fetch('/random/a/_payload.js', { responseType: 'text' })
|
||||
expect(payload).toMatch(
|
||||
/export default \{data:\{rand_a:\[[^\]]*\]\},prerenderedAt:\d*\}/
|
||||
/export default \{data:\{hey:{[^}]*},rand_a:\[[^\]]*\]\},prerenderedAt:\d*\}/
|
||||
)
|
||||
})
|
||||
|
||||
|
3
test/fixtures/basic/plugins/async-plugin.ts
vendored
3
test/fixtures/basic/plugins/async-plugin.ts
vendored
@ -1,11 +1,12 @@
|
||||
export default defineNuxtPlugin(async (/* nuxtApp */) => {
|
||||
const config1 = useRuntimeConfig()
|
||||
await new Promise(resolve => setTimeout(resolve, 100))
|
||||
const { data } = useFetch('/api/hey', { key: 'hey' })
|
||||
const config2 = useRuntimeConfig()
|
||||
return {
|
||||
provide: {
|
||||
asyncPlugin: () => config1 && config1 === config2
|
||||
? 'Async plugin works! ' + config1.testConfig
|
||||
? 'Async plugin works! ' + config1.testConfig + (data.value?.baz ? 'useFetch works!' : 'useFetch does not work')
|
||||
: 'Async plugin failed!'
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user