mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +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
|
// Server side
|
||||||
if (process.server && fetchOnServer && options.immediate) {
|
if (process.server && fetchOnServer && options.immediate) {
|
||||||
const promise = initialFetch()
|
const promise = initialFetch()
|
||||||
|
if (getCurrentInstance()) {
|
||||||
onServerPrefetch(() => promise)
|
onServerPrefetch(() => promise)
|
||||||
|
} else {
|
||||||
|
nuxt.hook('app:created', () => promise)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Client side
|
// Client side
|
||||||
|
@ -381,6 +381,7 @@ describe('plugins', () => {
|
|||||||
it('async plugin', async () => {
|
it('async plugin', async () => {
|
||||||
const html = await $fetch('/plugins')
|
const html = await $fetch('/plugins')
|
||||||
expect(html).toContain('asyncPlugin: Async plugin works! 123')
|
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 () => {
|
it('renders a payload', async () => {
|
||||||
const payload = await $fetch('/random/a/_payload.js', { responseType: 'text' })
|
const payload = await $fetch('/random/a/_payload.js', { responseType: 'text' })
|
||||||
expect(payload).toMatch(
|
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 */) => {
|
export default defineNuxtPlugin(async (/* nuxtApp */) => {
|
||||||
const config1 = useRuntimeConfig()
|
const config1 = useRuntimeConfig()
|
||||||
await new Promise(resolve => setTimeout(resolve, 100))
|
await new Promise(resolve => setTimeout(resolve, 100))
|
||||||
|
const { data } = useFetch('/api/hey', { key: 'hey' })
|
||||||
const config2 = useRuntimeConfig()
|
const config2 = useRuntimeConfig()
|
||||||
return {
|
return {
|
||||||
provide: {
|
provide: {
|
||||||
asyncPlugin: () => config1 && config1 === config2
|
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!'
|
: 'Async plugin failed!'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user