Skip to content

Commit 02421cd

Browse files
authored
fix(compiler-core): preserve vnode lifecycle in stable v-for (#11682)
close #9239 close #12569
1 parent 2468464 commit 02421cd

5 files changed

Lines changed: 321 additions & 17 deletions

File tree

‎packages/compiler-core/__tests__/transforms/transformElement.spec.ts‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ function parseWithBind(template: string, options?: CompilerOptions) {
7474
})
7575
}
7676

77+
function getVForChild(
78+
node: ReturnType<typeof parseWithForTransform>['node'],
79+
): VNodeCall {
80+
return node.codegenNode.children.arguments[1].returns as VNodeCall
81+
}
82+
7783
describe('compiler: element transform', () => {
7884
test('import + resolve component', () => {
7985
const { root } = parseWithElementTransform(`<Foo/>`)
@@ -1028,6 +1034,105 @@ describe('compiler: element transform', () => {
10281034
expect(node.patchFlag).toBe(PatchFlags.NEED_PATCH)
10291035
})
10301036

1037+
test('NEED_PATCH (stable v-for + static ref)', () => {
1038+
const { node } = parseWithForTransform(
1039+
`<div v-for="i in 3" :key="i" ref="foo" />`,
1040+
{ prefixIdentifiers: true },
1041+
)
1042+
const child = getVForChild(node)
1043+
expect(child.isBlock).toBe(false)
1044+
expect(child.patchFlag).toBe(PatchFlags.NEED_PATCH)
1045+
})
1046+
1047+
test('NEED_PATCH (stable v-for + custom directives)', () => {
1048+
const { node } = parseWithForTransform(
1049+
`<div v-for="i in 3" :key="i" v-dir />`,
1050+
{ prefixIdentifiers: true },
1051+
)
1052+
const child = getVForChild(node)
1053+
expect(child.isBlock).toBe(false)
1054+
expect(child.patchFlag).toBe(PatchFlags.NEED_PATCH)
1055+
})
1056+
1057+
test('NEED_PATCH (stable v-for + vnode hooks)', () => {
1058+
const { node } = parseWithForTransform(
1059+
`<div v-for="i in 3" :key="i" @vue:unmounted="foo" />`,
1060+
{
1061+
prefixIdentifiers: true,
1062+
cacheHandlers: true,
1063+
directiveTransforms: {
1064+
bind: transformBind,
1065+
on: transformOn,
1066+
},
1067+
},
1068+
)
1069+
const child = getVForChild(node)
1070+
expect(child.isBlock).toBe(false)
1071+
expect(child.patchFlag).toBe(PatchFlags.NEED_PATCH)
1072+
})
1073+
1074+
test('NEED_PATCH (stable v-for + setup-const function ref)', () => {
1075+
const { node } = parseWithForTransform(
1076+
`<div v-for="i in 3" :key="i" :ref="setRefFn" />`,
1077+
{
1078+
prefixIdentifiers: true,
1079+
bindingMetadata: {
1080+
setRefFn: BindingTypes.SETUP_CONST,
1081+
},
1082+
},
1083+
)
1084+
const child = getVForChild(node)
1085+
expect(child.isBlock).toBe(false)
1086+
expect(child.patchFlag).toBe(PatchFlags.NEED_PATCH)
1087+
})
1088+
1089+
test('preserve block for stable v-for + custom directive with children', () => {
1090+
const { node } = parseWithForTransform(
1091+
`<div v-for="i in 3" v-dir>{{ i }}</div>`,
1092+
{ prefixIdentifiers: true },
1093+
)
1094+
const child = getVForChild(node)
1095+
expect(child.isBlock).toBe(true)
1096+
expect(child.patchFlag).toBe(PatchFlags.TEXT)
1097+
})
1098+
1099+
test.each(['before-update', 'beforeUpdate'])(
1100+
'preserve block for stable v-for + @vue:%s vnode hook',
1101+
hook => {
1102+
const { node } = parseWithForTransform(
1103+
`<div v-for="i in 3" @vue:${hook}="foo"><span /></div>`,
1104+
{
1105+
prefixIdentifiers: true,
1106+
cacheHandlers: true,
1107+
directiveTransforms: {
1108+
bind: transformBind,
1109+
on: transformOn,
1110+
},
1111+
},
1112+
)
1113+
const child = getVForChild(node)
1114+
expect(child.isBlock).toBe(true)
1115+
expect(child.patchFlag).toBeUndefined()
1116+
},
1117+
)
1118+
1119+
test('does not add NEED_PATCH when another patch flag tracks the vnode', () => {
1120+
const { node } = parseWithForTransform(
1121+
`<div v-for="i in 3" :key="i" @vue:unmounted="foo">{{ i }}</div>`,
1122+
{
1123+
prefixIdentifiers: true,
1124+
cacheHandlers: true,
1125+
directiveTransforms: {
1126+
bind: transformBind,
1127+
on: transformOn,
1128+
},
1129+
},
1130+
)
1131+
const child = getVForChild(node)
1132+
expect(child.isBlock).toBe(false)
1133+
expect(child.patchFlag).toBe(PatchFlags.TEXT)
1134+
})
1135+
10311136
test('script setup inline mode template ref (binding exists)', () => {
10321137
const { node } = parseWithElementTransform(`<input ref="input"/>`, {
10331138
inline: true,

‎packages/compiler-core/src/ast.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,10 @@ export interface VNodeCall extends Node {
330330
patchFlag: PatchFlags | undefined
331331
dynamicProps: string | SimpleExpressionNode | undefined
332332
directives: DirectiveArguments | undefined
333+
/** Whether this vnode must be patched if a later transform makes it non-block. */
334+
needsPatch?: boolean
335+
/** Whether a later transform must preserve this vnode as a block. */
336+
isBlockRequired?: boolean
333337
isBlock: boolean
334338
disableTracking: boolean
335339
isComponent: boolean

‎packages/compiler-core/src/transforms/transformElement.ts‎

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ export const transformElement: NodeTransform = (node, context) => {
102102
let vnodeDynamicProps: VNodeCall['dynamicProps']
103103
let dynamicPropNames: string[] | undefined
104104
let vnodeDirectives: VNodeCall['directives']
105+
let needsPatch = false
106+
let isBlockRequired = false
105107

106108
let shouldUseBlock =
107109
// dynamic component may resolve to plain elements
@@ -127,6 +129,8 @@ export const transformElement: NodeTransform = (node, context) => {
127129
vnodeProps = propsBuildResult.props
128130
patchFlag = propsBuildResult.patchFlag
129131
dynamicPropNames = propsBuildResult.dynamicPropNames
132+
needsPatch = propsBuildResult.needsPatch
133+
isBlockRequired = propsBuildResult.isBlockRequired
130134
const directives = propsBuildResult.directives
131135
vnodeDirectives =
132136
directives && directives.length
@@ -206,7 +210,7 @@ export const transformElement: NodeTransform = (node, context) => {
206210
vnodeDynamicProps = stringifyDynamicPropNames(dynamicPropNames)
207211
}
208212

209-
node.codegenNode = createVNodeCall(
213+
const vnodeCall = (node.codegenNode = createVNodeCall(
210214
context,
211215
vnodeTag,
212216
vnodeProps,
@@ -218,7 +222,15 @@ export const transformElement: NodeTransform = (node, context) => {
218222
false /* disableTracking */,
219223
isComponent,
220224
node.loc,
221-
)
225+
))
226+
needsPatch =
227+
needsPatch && (patchFlag === 0 || patchFlag === PatchFlags.NEED_HYDRATION)
228+
if (needsPatch) {
229+
vnodeCall.needsPatch = true
230+
}
231+
if (isBlockRequired) {
232+
vnodeCall.isBlockRequired = true
233+
}
222234
}
223235
}
224236

@@ -382,13 +394,16 @@ export function buildProps(
382394
patchFlag: number
383395
dynamicPropNames: string[]
384396
shouldUseBlock: boolean
397+
needsPatch: boolean
398+
isBlockRequired: boolean
385399
} {
386400
const { tag, loc: elementLoc, children } = node
387401
let properties: ObjectExpression['properties'] = []
388402
const mergeArgs: PropsExpression[] = []
389403
const runtimeDirectives: DirectiveNode[] = []
390404
const hasChildren = children.length > 0
391405
let shouldUseBlock = false
406+
let isBlockRequired = false
392407

393408
// patchFlag analysis
394409
let patchFlag = 0
@@ -444,6 +459,10 @@ export function buildProps(
444459
hasVnodeHook = true
445460
}
446461

462+
if (name === 'ref') {
463+
hasRef = true
464+
}
465+
447466
if (isEventHandler && value.type === NodeTypes.JS_CALL_EXPRESSION) {
448467
// handler wrapped with internal helper e.g. withModifiers(fn)
449468
// extract the actual expression
@@ -460,13 +479,15 @@ export function buildProps(
460479
return
461480
}
462481

463-
if (name === 'ref') {
464-
hasRef = true
465-
} else if (name === 'class') {
482+
if (name === 'class') {
466483
hasClassBinding = true
467484
} else if (name === 'style') {
468485
hasStyleBinding = true
469-
} else if (name !== 'key' && !dynamicPropNames.includes(name)) {
486+
} else if (
487+
name !== 'ref' &&
488+
name !== 'key' &&
489+
!dynamicPropNames.includes(name)
490+
) {
470491
dynamicPropNames.push(name)
471492
}
472493

@@ -573,14 +594,21 @@ export function buildProps(
573594
continue
574595
}
575596

597+
// #938: elements with dynamic keys should be forced into blocks
598+
if (isVBind && isStaticArgOf(arg, 'key')) {
599+
shouldUseBlock = true
600+
}
601+
// inline before-update hooks need to remain blocks so that they are
602+
// invoked before children
576603
if (
577-
// #938: elements with dynamic keys should be forced into blocks
578-
(isVBind && isStaticArgOf(arg, 'key')) ||
579-
// inline before-update hooks need to force block so that it is invoked
580-
// before children
581-
(isVOn && hasChildren && isStaticArgOf(arg, 'vue:before-update'))
604+
isVOn &&
605+
hasChildren &&
606+
arg &&
607+
isStaticExp(arg) &&
608+
camelize(arg.content) === 'vue:beforeUpdate'
582609
) {
583610
shouldUseBlock = true
611+
isBlockRequired = true
584612
}
585613

586614
if (isVBind && isStaticArgOf(arg, 'ref')) {
@@ -691,6 +719,7 @@ export function buildProps(
691719
// to ensure before-update gets called before children update
692720
if (hasChildren) {
693721
shouldUseBlock = true
722+
isBlockRequired = true
694723
}
695724
}
696725
}
@@ -736,11 +765,10 @@ export function buildProps(
736765
patchFlag |= PatchFlags.NEED_HYDRATION
737766
}
738767
}
739-
if (
740-
!shouldUseBlock &&
768+
const needsPatch =
741769
(patchFlag === 0 || patchFlag === PatchFlags.NEED_HYDRATION) &&
742770
(hasRef || hasVnodeHook || runtimeDirectives.length > 0)
743-
) {
771+
if (!shouldUseBlock && needsPatch) {
744772
patchFlag |= PatchFlags.NEED_PATCH
745773
}
746774

@@ -825,6 +853,8 @@ export function buildProps(
825853
patchFlag,
826854
dynamicPropNames,
827855
shouldUseBlock,
856+
needsPatch,
857+
isBlockRequired,
828858
}
829859
}
830860

‎packages/compiler-core/src/transforms/vFor.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,9 @@ export const transformFor: NodeTransform = createStructuralDirectiveTransform(
186186
if (isTemplate && keyProperty) {
187187
injectProp(childBlock, keyProperty, context)
188188
}
189-
if (childBlock.isBlock !== !isStableFragment) {
189+
const shouldUseBlock =
190+
!isStableFragment || childBlock.isBlockRequired === true
191+
if (childBlock.isBlock !== shouldUseBlock) {
190192
if (childBlock.isBlock) {
191193
// switch from block to vnode
192194
removeHelper(OPEN_BLOCK)
@@ -200,12 +202,16 @@ export const transformFor: NodeTransform = createStructuralDirectiveTransform(
200202
)
201203
}
202204
}
203-
childBlock.isBlock = !isStableFragment
205+
childBlock.isBlock = shouldUseBlock
204206
if (childBlock.isBlock) {
205207
helper(OPEN_BLOCK)
206208
helper(getVNodeBlockHelper(context.inSSR, childBlock.isComponent))
207209
} else {
208210
helper(getVNodeHelper(context.inSSR, childBlock.isComponent))
211+
if (childBlock.needsPatch) {
212+
childBlock.patchFlag = ((childBlock.patchFlag ?? 0) |
213+
PatchFlags.NEED_PATCH) as PatchFlags
214+
}
209215
}
210216
}
211217

0 commit comments

Comments
 (0)