Skip to content

Commit b00fd3c

Browse files
committed
fix(hmr): fix render error in change static nodes (#6978)
1 parent dbe7109 commit b00fd3c

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
@@ -537,4 +537,32 @@ describe('hot module replacement', () => {
537537
render(h(Foo), root)
538538
expect(serializeInner(root)).toBe('bar')
539539
})
540+
541+
test('rerender in change hoisted nodes', () => {
542+
const root = nodeOps.createElement('div')
543+
const appId = 'test-app-id'
544+
const App: ComponentOptions = {
545+
__hmrId: appId,
546+
render: compileToFunction(
547+
`<div v-for="item of 2"><div>1</div></div><p>2</p><p>3</p>`
548+
)
549+
}
550+
createRecord(appId, App)
551+
552+
render(h(App), root)
553+
expect(serializeInner(root)).toBe(
554+
`<div><div>1</div></div><div><div>1</div></div><p>2</p><p>3</p>`
555+
)
556+
557+
// move the <p>3</p> into the <div>1</div>
558+
rerender(
559+
appId,
560+
compileToFunction(
561+
`<div v-for="item of 2"><div>1<p>3</p></div></div><p>2</p>`
562+
)
563+
)
564+
expect(serializeInner(root)).toBe(
565+
`<div><div>1<p>3</p></div></div><div><div>1<p>3</p></div></div><p>2</p>`
566+
)
567+
})
540568
})

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)