Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/runtime-core/__tests__/hmr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,38 @@ describe('hot module replacement', () => {
expect(mountSpy).toHaveBeenCalledTimes(1)
})

test('reload multiple children under same parent should rerender parent once', async () => {
const root = nodeOps.createElement('div')
const childId = 'test-reload-multiple-children-same-parent'

const Child: ComponentOptions = {
__hmrId: childId,
render: () => h('div', 'old'),
}
createRecord(childId, Child)

let parentRenderCount = 0
const Parent: ComponentOptions = {
render() {
parentRenderCount++
return [h(Child), h(Child)]
},
}

render(h(Parent), root)
expect(serializeInner(root)).toBe(`<div>old</div><div>old</div>`)
expect(parentRenderCount).toBe(1)

reload(childId, {
__hmrId: childId,
render: () => h('div', 'new'),
})
await nextTick()

expect(serializeInner(root)).toBe(`<div>new</div><div>new</div>`)
expect(parentRenderCount).toBe(2)
})

// #7042
test('reload KeepAlive slot', async () => {
const root = nodeOps.createElement('div')
Expand Down
88 changes: 70 additions & 18 deletions packages/runtime-core/src/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ function normalizeClassComponent(component: HMRComponent): ComponentOptions {
return isClassComponent(component) ? component.__vccOpts : component
}

function hasDirtyAncestor(
instance: GenericComponentInstance,
dirtyInstances: Set<GenericComponentInstance>,
): boolean {
let parent = instance.parent
while (parent) {
if (dirtyInstances.has(parent)) {
return true
}
parent = parent.parent
}
return false
}

function rerender(id: string, newRender?: Function): void {
const record = map.get(id)
if (!record) {
Expand Down Expand Up @@ -143,7 +157,13 @@ function reload(id: string, newComp: HMRComponent): void {
// create a snapshot which avoids the set being mutated during updates
const instances = [...record.instances]

if (isVapor && newComp.__vapor && !instances.some(i => i.ceReload)) {
if (
isVapor &&
newComp.__vapor &&
// VDOM parents need the VDOM HMR path to remount dirty Vapor children.
!instances.some(instance => instance.parent && !instance.parent.vapor) &&
!instances.some(i => i.ceReload)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
) {
// For multiple instances with the same __hmrId, remove styles first before reload
// to avoid the second instance's style removal deleting the first instance's
// newly added styles (since hmrReload is synchronous)
Expand All @@ -153,10 +173,30 @@ function reload(id: string, newComp: HMRComponent): void {
instance.root.ce._removeChildStyle(instance.type)
}
}
const dirtyInstances = new Set(instances)
const rerenderedParents = new Set<GenericComponentInstance>()
for (const instance of instances) {
instance.hmrReload!(newComp)
const parent = instance.parent
if (parent) {
// A dirty ancestor will recreate this child. Otherwise, each parent
// only needs one rerender for this HMR record.
if (
!hasDirtyAncestor(instance, dirtyInstances) &&
!rerenderedParents.has(parent)
) {
rerenderedParents.add(parent)
parent.hmrRerender!()
}
} else {
instance.hmrReload!(newComp)
}
}
} else {
const parentUpdates = new Map<
GenericComponentInstance,
[GenericComponentInstance, Set<GenericComponentInstance>][]
>()
const dirtyInstanceSet = new Set(instances)
for (const instance of instances as ComponentInternalInstance[]) {
const oldComp = normalizeClassComponent(instance.type as HMRComponent)

Expand Down Expand Up @@ -188,23 +228,14 @@ function reload(id: string, newComp: HMRComponent): void {
// 4. Force the parent instance to re-render. This will cause all updated
// components to be unmounted and re-mounted. Queue the update so that we
// don't end up forcing the same parent to re-render multiple times.
queueJob(() => {
isHmrUpdating = true
const parent = instance.parent! as ComponentInternalInstance
if (parent.vapor) {
parent.hmrRerender!()
} else {
if (!(parent.effect.flags! & EffectFlags.STOP)) {
parent.renderCache = []
parent.effect.run()
}
const parent = instance.parent
if (!hasDirtyAncestor(instance, dirtyInstanceSet)) {
let updates = parentUpdates.get(parent)
if (!updates) {
parentUpdates.set(parent, (updates = []))
}
nextTick(() => {
isHmrUpdating = false
})
// #6930, #11248 avoid infinite recursion
dirtyInstances.delete(instance)
})
updates.push([instance, dirtyInstances])
}
} else if (instance.appContext.reload) {
// root instance mounted via createApp() has a reload method
instance.appContext.reload()
Expand All @@ -222,6 +253,27 @@ function reload(id: string, newComp: HMRComponent): void {
instance.root.ce._removeChildStyle(oldComp)
}
}
parentUpdates.forEach((updates, parent) => {
queueJob(() => {
isHmrUpdating = true
if (parent.vapor) {
parent.hmrRerender!()
} else {
const i = parent as ComponentInternalInstance
if (!(i.effect.flags! & EffectFlags.STOP)) {
i.renderCache = []
i.effect.run()
}
}
nextTick(() => {
isHmrUpdating = false
})
// #6930, #11248 avoid infinite recursion
updates.forEach(([instance, dirtyInstances]) => {
dirtyInstances.delete(instance)
})
})
})
}
// 5. make sure to cleanup dirty hmr components after update
queuePostFlushCb(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ describe('renderer: VaporTeleport', () => {
return [n0]
},
})
await nextTick()
expect(root.innerHTML).toBe(
'<!--teleport start--><!--teleport end--><div>root</div>',
)
Expand Down
Loading
Loading