Skip to content

Commit 84f81a4

Browse files
committed
fix(runtime-vapor): properly handle consecutive prepend operations
1 parent bb4ae25 commit 84f81a4

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

packages/runtime-vapor/src/block.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ export function isValidBlock(block: Block): boolean {
105105

106106
export function insert(
107107
block: Block,
108-
parent: ParentNode,
108+
parent: ParentNode & { $anchor?: Node | null },
109109
anchor: Node | null | 0 = null, // 0 means prepend
110110
): void {
111-
anchor = anchor === 0 ? parent.firstChild : anchor
111+
anchor = anchor === 0 ? parent.$anchor || parent.firstChild : anchor
112112
if (block instanceof Node) {
113113
if (!isHydrating) {
114114
parent.insertBefore(block, anchor)

packages/runtime-vapor/src/insertionState.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@ export let insertionAnchor: Node | 0 | undefined
66
* (component, slot outlet, if, for) is created. The state is used for actual
77
* insertion on client-side render, and used for node adoption during hydration.
88
*/
9-
export function setInsertionState(parent: ParentNode, anchor?: Node | 0): void {
9+
export function setInsertionState(
10+
parent: ParentNode & { $anchor?: Node | null },
11+
anchor?: Node | 0,
12+
): void {
13+
// When setInsertionState(n3, 0) is called consecutively, the first prepend operation
14+
// uses parent.firstChild as the anchor. However, after insertion, parent.firstChild
15+
// changes and cannot serve as the anchor for subsequent prepends. Therefore, we cache
16+
// the original parent.firstChild on the first call for subsequent prepend operations.
17+
if (anchor === 0 && !parent.$anchor) {
18+
parent.$anchor = parent.firstChild
19+
}
20+
1021
insertionParent = parent
1122
insertionAnchor = anchor
1223
}

0 commit comments

Comments
 (0)