]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-core): properly check forwarded slots type (#3781)
authorHcySunYang <HcySunYang@outlook.com>
Wed, 26 May 2021 15:52:03 +0000 (23:52 +0800)
committerGitHub <noreply@github.com>
Wed, 26 May 2021 15:52:03 +0000 (11:52 -0400)
fix #3779

packages/runtime-core/__tests__/rendererOptimizedMode.spec.ts
packages/runtime-core/src/vnode.ts

index 8c28d88102dab531ca29cbadf64603d00e7a185e..ef4f3ef8355614c6af89a85bc26f3e02108fbad0 100644 (file)
@@ -734,4 +734,54 @@ describe('renderer: optimized mode', () => {
     await nextTick()
     expect(inner(root)).toBe('<p>1</p>')
   })
+
+  // #3779
+  test('treat slots manually written by the user as dynamic', async () => {
+    const Middle = {
+      setup(props: any, { slots }: any) {
+        return slots.default!
+      }
+    }
+
+    const Comp = {
+      setup(props: any, { slots }: any) {
+        return () => {
+          return (
+            openBlock(),
+            createBlock('div', null, [
+              createVNode(Middle, null, {
+                default: withCtx(
+                  () => [
+                    createVNode('div', null, [renderSlot(slots, 'default')])
+                  ],
+                  undefined
+                ),
+                _: 3 /* FORWARDED */
+              })
+            ])
+          )
+        }
+      }
+    }
+
+    const loading = ref(false)
+    const app = createApp({
+      setup() {
+        return () => {
+          // important: write the slot content here
+          const content = h('span', loading.value ? 'loading' : 'loaded')
+          return h(Comp, null, {
+            default: () => content
+          })
+        }
+      }
+    })
+
+    app.mount(root)
+    expect(inner(root)).toBe('<div><div><span>loaded</span></div></div>')
+
+    loading.value = true
+    await nextTick()
+    expect(inner(root)).toBe('<div><div><span>loading</span></div></div>')
+  })
 })
index 7e5976e2f7beac85cab34d393c9e2f1fa0219802..99adae44c01874cec4f99b8543951995a9a21196 100644 (file)
@@ -651,12 +651,12 @@ export function normalizeChildren(vnode: VNode, children: unknown) {
         // a child component receives forwarded slots from the parent.
         // its slot type is determined by its parent's slot type.
         if (
-          currentRenderingInstance.vnode.patchFlag & PatchFlags.DYNAMIC_SLOTS
+          (currentRenderingInstance.slots as RawSlots)._ === SlotFlags.STABLE
         ) {
+          ;(children as RawSlots)._ = SlotFlags.STABLE
+        } else {
           ;(children as RawSlots)._ = SlotFlags.DYNAMIC
           vnode.patchFlag |= PatchFlags.DYNAMIC_SLOTS
-        } else {
-          ;(children as RawSlots)._ = SlotFlags.STABLE
         }
       }
     }