Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(hmr): fix render error in change static nodes (#6978) #7114

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 30 additions & 1 deletion packages/runtime-core/__tests__/hmr.spec.ts
Expand Up @@ -21,7 +21,7 @@ const { createRecord, rerender, reload } = __VUE_HMR_RUNTIME__
registerRuntimeCompiler(compileToFunction)

function compileToFunction(template: string) {
const { code } = baseCompile(template)
const { code } = baseCompile(template, { hoistStatic: true })
const render = new Function('Vue', code)(
runtimeTest
) as InternalRenderFunction
Expand Down Expand Up @@ -568,4 +568,33 @@ describe('hot module replacement', () => {
rerender(parentId, compileToFunction(`<Child>2</Child>`))
expect(serializeInner(root)).toBe(`2`)
})

// #6978
test('rerender in change hoisted nodes', () => {
const root = nodeOps.createElement('div')
const appId = 'test-app-id'
const App: ComponentOptions = {
__hmrId: appId,
render: compileToFunction(
`<div v-for="item of 2"><div>1</div></div><p>2</p><p>3</p>`
)
}
createRecord(appId, App)

render(h(App), root)
expect(serializeInner(root)).toBe(
`<div><div>1</div></div><div><div>1</div></div><p>2</p><p>3</p>`
)

// move the <p>3</p> into the <div>1</div>
rerender(
appId,
compileToFunction(
`<div v-for="item of 2"><div>1<p>3</p></div></div><p>2</p>`
)
)
expect(serializeInner(root)).toBe(
`<div><div>1<p>3</p></div></div><div><div>1<p>3</p></div></div><p>2</p>`
)
})
})
6 changes: 6 additions & 0 deletions packages/runtime-core/src/vnode.ts
Expand Up @@ -423,6 +423,12 @@ function createBaseVNode(
isBlockNode = false,
needFullChildrenNormalization = false
) {
// #6978 the children maybe a hoisted array that should be cloned
// since it maybe changed during HMR
if (__DEV__ && isArray(children)) {
children = [...children]
}
Comment on lines +428 to +430
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we should do always this, I wonder if we should change children to be cloned: children = (children as VNode[]).map(deepCloneVNode)

I also wonder if the condition is correct, that way we are unnecessarily adding a new array every time, from this example alone we could do something like:

if (
    __DEV__ &&
    (patchFlag === PatchFlags.HOISTED ||
      patchFlag === PatchFlags.UNKEYED_FRAGMENT) &&
    isArray(children)
  )

But not certain if that will fix every case 🤔


const vnode = {
__v_isVNode: true,
__v_skip: true,
Expand Down