Skip to content

Commit

Permalink
fix(compiler-core): generate NEED_PATCH flag for element with vnode h…
Browse files Browse the repository at this point in the history
…ooks
  • Loading branch information
yyx990803 committed Aug 19, 2020
1 parent c2913d5 commit 24041b7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,11 @@ describe('compiler: element transform', () => {
expect(node.patchFlag).toBe(genFlagText(PatchFlags.NEED_PATCH))
})

test('NEED_PATCH (vnode hooks)', () => {
const { node } = parseWithBind(`<div @vnodeUpdated="foo" />`)
expect(node.patchFlag).toBe(genFlagText(PatchFlags.NEED_PATCH))
})

test('HYDRATE_EVENTS', () => {
// ignore click events (has dedicated fast path)
const { node } = parseWithElementTransform(`<div @click="foo" />`, {
Expand Down
18 changes: 14 additions & 4 deletions packages/compiler-core/src/transforms/transformElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import {
PatchFlagNames,
isSymbol,
isOn,
isObject
isObject,
isReservedProp
} from '@vue/shared'
import { createCompilerError, ErrorCodes } from '../errors'
import {
Expand Down Expand Up @@ -281,22 +282,31 @@ export function buildProps(
let hasStyleBinding = false
let hasHydrationEventBinding = false
let hasDynamicKeys = false
let hasVnodeHook = false
const dynamicPropNames: string[] = []

const analyzePatchFlag = ({ key, value }: Property) => {
if (isStaticExp(key)) {
const name = key.content
const isEventHandler = isOn(name)
if (
!isComponent &&
isOn(name) &&
isEventHandler &&
// omit the flag for click handlers because hydration gives click
// dedicated fast path.
name.toLowerCase() !== 'onclick' &&
// omit v-model handlers
name !== 'onUpdate:modelValue'
name !== 'onUpdate:modelValue' &&
// omit onVnodeXXX hooks
!isReservedProp(name)
) {
hasHydrationEventBinding = true
}

if (isEventHandler && isReservedProp(name)) {
hasVnodeHook = true
}

if (
value.type === NodeTypes.JS_CACHE_EXPRESSION ||
((value.type === NodeTypes.SIMPLE_EXPRESSION ||
Expand Down Expand Up @@ -475,7 +485,7 @@ export function buildProps(
}
if (
(patchFlag === 0 || patchFlag === PatchFlags.HYDRATE_EVENTS) &&
(hasRef || runtimeDirectives.length > 0)
(hasRef || hasVnodeHook || runtimeDirectives.length > 0)
) {
patchFlag |= PatchFlags.NEED_PATCH
}
Expand Down

0 comments on commit 24041b7

Please sign in to comment.