From 7044f6ff529cbc721a83ef65a2b3a365d2478805 Mon Sep 17 00:00:00 2001 From: daiwei Date: Tue, 14 Oct 2025 16:28:08 +0800 Subject: [PATCH] fix: handling v-for with non-hydration nodes --- .../runtime-vapor/__tests__/hydration.spec.ts | 51 +++++++++++++++++++ packages/runtime-vapor/src/apiCreateFor.ts | 10 ++-- 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/packages/runtime-vapor/__tests__/hydration.spec.ts b/packages/runtime-vapor/__tests__/hydration.spec.ts index a20c6f3f55..7de42143f4 100644 --- a/packages/runtime-vapor/__tests__/hydration.spec.ts +++ b/packages/runtime-vapor/__tests__/hydration.spec.ts @@ -1940,6 +1940,57 @@ describe('Vapor Mode hydration', () => { " `) }) + + test('with non-hydration node', async () => { + const data = ref({ show: true, msg: 'foo' }) + const { container } = await testHydration( + ``, + {}, + data, + ) + expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( + ` + "
+
foo
non-hydration node
foo
non-hydration node
+
" + `, + ) + + data.value.msg = 'bar' + await nextTick() + expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( + ` + "
+
bar
non-hydration node
bar
non-hydration node
+
" + `, + ) + + data.value.show = false + await nextTick() + expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(` + "
+
non-hydration node
non-hydration node
+
" + `) + + data.value.show = true + await nextTick() + expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(` + "
+
bar
non-hydration node
bar
non-hydration node
+
" + `) + }) }) describe('slots', () => { diff --git a/packages/runtime-vapor/src/apiCreateFor.ts b/packages/runtime-vapor/src/apiCreateFor.ts index 6f7bd2dca5..bdeccce2a5 100644 --- a/packages/runtime-vapor/src/apiCreateFor.ts +++ b/packages/runtime-vapor/src/apiCreateFor.ts @@ -132,18 +132,14 @@ export const createFor = ( if (!isMounted) { isMounted = true - let prevNodes: Block for (let i = 0; i < newLength; i++) { - if (isHydrating && isComponent && i > 0) { - setCurrentHydrationNode(findLastChild(prevNodes!)!.nextSibling) + const nodes = mount(source, i).nodes + if (isHydrating) { + setCurrentHydrationNode(findLastChild(nodes!)!.nextSibling) } - prevNodes = mount(source, i).nodes } if (isHydrating) { - if (isComponent) { - setCurrentHydrationNode(findLastChild(prevNodes!)!.nextSibling) - } parentAnchor = newLength === 0 ? currentHydrationNode!.nextSibling! -- 2.47.3