]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(hydration): skip dynamic children in __child
authordaiwei <daiwei521@126.com>
Fri, 4 Jul 2025 02:52:45 +0000 (10:52 +0800)
committerdaiwei <daiwei521@126.com>
Thu, 31 Jul 2025 00:14:47 +0000 (08:14 +0800)
packages/compiler-vapor/__tests__/__snapshots__/compile.spec.ts.snap
packages/compiler-vapor/src/generators/template.ts
packages/runtime-vapor/__tests__/hydration.spec.ts
packages/runtime-vapor/src/dom/node.ts

index b10a98d32cba702483ed45d2d0175ec8232ffc37..5ed49dc4e5ec969a2227349d1a059401241f3a60 100644 (file)
@@ -157,7 +157,7 @@ export function render(_ctx, $props, $emit, $attrs, $slots) {
   const _component_Comp = _resolveComponent("Comp")
   const n0 = t0()
   const n3 = t1()
-  const n2 = _child(n3)
+  const n2 = _child(n3, 1)
   _setInsertionState(n3, 0)
   const n1 = _createComponentWithFallback(_component_Comp)
   _renderEffect(() => {
index 9e2b810610b2ea35f24c2565fa53f777800998f1..0229649cf0805a46a6ed5687effa9ced372f1568 100644 (file)
@@ -82,11 +82,15 @@ export function genChildren(
         pushBlock(...genCall(helper('nthChild'), from, String(elementIndex)))
       }
     } else {
+      // offset is used to determine the child during hydration.
+      // if offset is not 0, we need to specify the offset to skip the dynamic
+      // children and get the correct child.
+      let childOffset = offset === 0 ? undefined : `${Math.abs(offset)}`
       if (elementIndex === 0) {
-        pushBlock(...genCall(helper('child'), from))
+        pushBlock(...genCall(helper('child'), from, childOffset))
       } else {
         // check if there's a node that we can reuse from
-        let init = genCall(helper('child'), from)
+        let init = genCall(helper('child'), from, childOffset)
         if (elementIndex === 1) {
           init = genCall(helper('next'), init)
         } else if (elementIndex > 1) {
index 42f3add505c2f1906ec00d5b95c8743c14253b2c..e854a193603b223f76cded796c7dd5d928f9bda4 100644 (file)
@@ -2176,6 +2176,43 @@ describe('Vapor Mode hydration', () => {
       )
     })
 
+    test('mixed consecutive slot and element', async () => {
+      const data = reactive({
+        text: 'foo',
+        msg: 'hi',
+      })
+      const { container } = await testHydration(
+        `<template>
+          <components.Child>
+            <template #foo><span>{{data.text}}</span></template>
+            <template #bar><span>bar</span></template>
+          </components.Child>
+        </template>`,
+        {
+          Child: `<template><div><slot name="foo"/><slot name="bar"/><div>{{data.msg}}</div></div></template>`,
+        },
+        data,
+      )
+
+      expect(container.innerHTML).toBe(
+        `<div>` +
+          `<!--[--><span>foo</span><!--]--><!--${slotAnchorLabel}-->` +
+          `<!--[--><span>bar</span><!--]--><!--${slotAnchorLabel}-->` +
+          `<div>hi</div>` +
+          `</div>`,
+      )
+
+      data.msg = 'bar'
+      await nextTick()
+      expect(container.innerHTML).toBe(
+        `<div>` +
+          `<!--[--><span>foo</span><!--]--><!--${slotAnchorLabel}-->` +
+          `<!--[--><span>bar</span><!--]--><!--${slotAnchorLabel}-->` +
+          `<div>bar</div>` +
+          `</div>`,
+      )
+    })
+
     test('mixed slot and element', async () => {
       const data = reactive({
         text: 'foo',
index 4c613324e12aedc0f2097901fc05f7a036c4c653..7622c1676d0b799abae6cd0489a9f6cd44e3f02d 100644 (file)
@@ -41,7 +41,7 @@ export function _child(node: ParentNode): Node {
  *
  *   Client Compiled Code (Simplified):
  *     const n2 = t0() // n2 = `<div> </div>`
- *     const n1 = _child(n2) // n1 = text node
+ *     const n1 = _child(n2, 1) // n1 = text node
  *     // ... slot creation ...
  *     _renderEffect(() => _setText(n1, _ctx.msg))
  *
@@ -49,18 +49,18 @@ export function _child(node: ParentNode): Node {
  *
  *   Hydration Mismatch:
  *   - During hydration, `n2` refers to the SSR `<div>`.
- *   - `_child(n2)` would return `<!--[-->`.
+ *   - `_child(n2, 1)` would return `<!--[-->`.
  *   - The client code expects `n1` to be the text node, but gets the comment.
  *     The subsequent `_setText(n1, ...)` would fail or target the wrong node.
  *
  *   Solution (`__child`):
- *   - `__child(n2)` is used during hydration. It skips the SSR fragment anchors
- *     (`<!--[-->...<!--]-->`) and any other non-content nodes to find the
- *     "Actual Text Node", correctly matching the client's expectation for `n1`.
+ *   - `__child(n2, offset)` is used during hydration. It skips the dynamic children
+ *     to find the "Actual Text Node", correctly matching the client's expectation
+ *     for `n1`.
  */
 /*! #__NO_SIDE_EFFECTS__ */
-export function __child(node: ParentNode): Node {
-  let n = node.firstChild!
+export function __child(node: ParentNode, offset?: number): Node {
+  let n = offset ? __nthChild(node, offset) : node.firstChild!
 
   if (isComment(n, '[')) {
     n = locateEndAnchor(n)!.nextSibling!
@@ -162,8 +162,8 @@ type DelegatedFunction<T extends (...args: any[]) => any> = T & {
 }
 
 /*! #__NO_SIDE_EFFECTS__ */
-export const child: DelegatedFunction<typeof _child> = node => {
-  return child.impl(node)
+export const child: DelegatedFunction<typeof __child> = (node, offset) => {
+  return child.impl(node, offset)
 }
 child.impl = _child