Skip to content

Commit 053c65b

Browse files
authored
dx(suspense): warn when using async setup when not inside a Suspense boundary (#5565)
close #3649
1 parent 57ca32b commit 053c65b

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

packages/runtime-core/__tests__/components/Suspense.spec.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ describe('Suspense', () => {
709709
<div v-if="errorMessage">{{ errorMessage }}</div>
710710
<Suspense v-else>
711711
<div>
712-
<Async />
712+
<Async />
713713
</div>
714714
<template #fallback>
715715
<div>fallback</div>
@@ -1232,4 +1232,25 @@ describe('Suspense', () => {
12321232
await nextTick()
12331233
expect(serializeInner(root)).toBe(`<div>parent<!----></div>`)
12341234
})
1235+
1236+
test('warn if using async setup when not in a Suspense boundary', () => {
1237+
const Child = {
1238+
name: 'Child',
1239+
async setup() {
1240+
return () => h('div', 'child')
1241+
}
1242+
}
1243+
const Parent = {
1244+
setup() {
1245+
return () => h('div', [h(Child)])
1246+
}
1247+
}
1248+
1249+
const root = nodeOps.createElement('div')
1250+
render(h(Parent), root)
1251+
1252+
expect(
1253+
`A component with async setup() must be nested in a <Suspense>`
1254+
).toHaveBeenWarned()
1255+
})
12351256
})

packages/runtime-core/src/component.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,6 @@ function setupStatefulComponent(
654654

655655
if (isPromise(setupResult)) {
656656
setupResult.then(unsetCurrentInstance, unsetCurrentInstance)
657-
658657
if (isSSR) {
659658
// return the promise so server-renderer can wait on it
660659
return setupResult
@@ -668,6 +667,15 @@ function setupStatefulComponent(
668667
// async setup returned Promise.
669668
// bail here and wait for re-entry.
670669
instance.asyncDep = setupResult
670+
if (__DEV__ && !instance.suspense) {
671+
const name = Component.name ?? 'Anonymous'
672+
warn(
673+
`Component <${name}>: setup function returned a promise, but no ` +
674+
`<Suspense> boundary was found in the parent component tree. ` +
675+
`A component with async setup() must be nested in a <Suspense> ` +
676+
`in order to be rendered.`
677+
)
678+
}
671679
} else if (__DEV__) {
672680
warn(
673681
`setup() returned a Promise, but the version of Vue you are using ` +

0 commit comments

Comments
 (0)