]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
dx(suspense): warn when using async setup when not inside a Suspense boundary (#5565)
authorThorsten Lünborg <t.luenborg@googlemail.com>
Wed, 13 Apr 2022 09:36:43 +0000 (11:36 +0200)
committerGitHub <noreply@github.com>
Wed, 13 Apr 2022 09:36:43 +0000 (05:36 -0400)
close #3649

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

index 421bc0a8e963b00ef04fda3c032a413174952519..b206dced46349235a20f75eb9ea704c7a74d4f07 100644 (file)
@@ -709,7 +709,7 @@ describe('Suspense', () => {
       <div v-if="errorMessage">{{ errorMessage }}</div>
       <Suspense v-else>
         <div>
-          <Async />     
+          <Async />
         </div>
         <template #fallback>
           <div>fallback</div>
@@ -1232,4 +1232,25 @@ describe('Suspense', () => {
     await nextTick()
     expect(serializeInner(root)).toBe(`<div>parent<!----></div>`)
   })
+
+  test('warn if using async setup when not in a Suspense boundary', () => {
+    const Child = {
+      name: 'Child',
+      async setup() {
+        return () => h('div', 'child')
+      }
+    }
+    const Parent = {
+      setup() {
+        return () => h('div', [h(Child)])
+      }
+    }
+
+    const root = nodeOps.createElement('div')
+    render(h(Parent), root)
+
+    expect(
+      `A component with async setup() must be nested in a <Suspense>`
+    ).toHaveBeenWarned()
+  })
 })
index 9b2f35c9745d71ee3cb1a0d10cc2c75482ecad1a..53048b9a294a685e0264ec02d3925b9afec5c7fa 100644 (file)
@@ -654,7 +654,6 @@ function setupStatefulComponent(
 
     if (isPromise(setupResult)) {
       setupResult.then(unsetCurrentInstance, unsetCurrentInstance)
-
       if (isSSR) {
         // return the promise so server-renderer can wait on it
         return setupResult
@@ -668,6 +667,15 @@ function setupStatefulComponent(
         // async setup returned Promise.
         // bail here and wait for re-entry.
         instance.asyncDep = setupResult
+        if (__DEV__ && !instance.suspense) {
+          const name = Component.name ?? 'Anonymous'
+          warn(
+            `Component <${name}>: setup function returned a promise, but no ` +
+              `<Suspense> boundary was found in the parent component tree. ` +
+              `A component with async setup() must be nested in a <Suspense> ` +
+              `in order to be rendered.`
+          )
+        }
       } else if (__DEV__) {
         warn(
           `setup() returned a Promise, but the version of Vue you are using ` +