Skip to content

Commit 940ee69

Browse files
committed
fix(hmr): fix render error in change static nodes (#6978)
1 parent 01f43c1 commit 940ee69

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

packages/runtime-core/__tests__/hmr.spec.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const { createRecord, rerender, reload } = __VUE_HMR_RUNTIME__
2121
registerRuntimeCompiler(compileToFunction)
2222

2323
function compileToFunction(template: string) {
24-
const { code } = baseCompile(template)
24+
const { code } = baseCompile(template, { hoistStatic: true })
2525
const render = new Function('Vue', code)(
2626
runtimeTest
2727
) as InternalRenderFunction
@@ -568,4 +568,32 @@ describe('hot module replacement', () => {
568568
rerender(parentId, compileToFunction(`<Child>2</Child>`))
569569
expect(serializeInner(root)).toBe(`2`)
570570
})
571+
572+
test('rerender in change hoisted nodes', () => {
573+
const root = nodeOps.createElement('div')
574+
const appId = 'test-app-id'
575+
const App: ComponentOptions = {
576+
__hmrId: appId,
577+
render: compileToFunction(
578+
`<div v-for="item of 2"><div>1</div></div><p>2</p><p>3</p>`
579+
)
580+
}
581+
createRecord(appId, App)
582+
583+
render(h(App), root)
584+
expect(serializeInner(root)).toBe(
585+
`<div><div>1</div></div><div><div>1</div></div><p>2</p><p>3</p>`
586+
)
587+
588+
// move the <p>3</p> into the <div>1</div>
589+
rerender(
590+
appId,
591+
compileToFunction(
592+
`<div v-for="item of 2"><div>1<p>3</p></div></div><p>2</p>`
593+
)
594+
)
595+
expect(serializeInner(root)).toBe(
596+
`<div><div>1<p>3</p></div></div><div><div>1<p>3</p></div></div><p>2</p>`
597+
)
598+
})
571599
})

packages/runtime-core/src/vnode.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import {
3939
} from './componentRenderContext'
4040
import { RendererNode, RendererElement } from './renderer'
4141
import { NULL_DYNAMIC_COMPONENT } from './helpers/resolveAssets'
42-
import { hmrDirtyComponents } from './hmr'
42+
import { hmrDirtyComponents, isHmrUpdating } from './hmr'
4343
import { convertLegacyComponent } from './compat/component'
4444
import { convertLegacyVModelProps } from './compat/componentVModel'
4545
import { defineLegacyVNodeProperties } from './compat/renderFn'
@@ -423,6 +423,12 @@ function createBaseVNode(
423423
isBlockNode = false,
424424
needFullChildrenNormalization = false
425425
) {
426+
// #6978 the children maybe a hoisted array that should be cloned
427+
// since it maybe changed during HMR
428+
if (__DEV__ && isHmrUpdating && isArray(children)) {
429+
children = [...children]
430+
}
431+
426432
const vnode = {
427433
__v_isVNode: true,
428434
__v_skip: true,

0 commit comments

Comments
 (0)