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(runtime-core): hydration should not be performed during the HMR reload process(fix #7706) #8170

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions packages/runtime-core/src/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { extend, getGlobalThis } from '@vue/shared'
type HMRComponent = ComponentOptions | ClassComponent

export let isHmrUpdating = false
export let isHmrReloading = false

export const hmrDirtyComponents = new Set<ConcreteComponent>()

Expand Down Expand Up @@ -127,6 +128,8 @@ function reload(id: string, newComp: HMRComponent) {
instance.appContext.emitsCache.delete(instance.type as any)
instance.appContext.optionsCache.delete(instance.type as any)

isHmrReloading = true

// 4. actually update
if (instance.ceReload) {
// custom element
Expand All @@ -153,6 +156,7 @@ function reload(id: string, newComp: HMRComponent) {

// 5. make sure to cleanup dirty hmr components after update
queuePostFlushCb(() => {
isHmrReloading = false
for (const instance of instances) {
hmrDirtyComponents.delete(
normalizeClassComponent(instance.type as HMRComponent)
Expand Down
9 changes: 7 additions & 2 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ import {
} from './components/Suspense'
import { TeleportImpl, TeleportVNode } from './components/Teleport'
import { isKeepAlive, KeepAliveContext } from './components/KeepAlive'
import { registerHMR, unregisterHMR, isHmrUpdating } from './hmr'
import {
registerHMR,
unregisterHMR,
isHmrUpdating,
isHmrReloading
} from './hmr'
import { createHydrationFunctions, RootHydrateFunction } from './hydration'
import { invokeDirectiveHook } from './directives'
import { startMeasure, endMeasure } from './profiling'
Expand Down Expand Up @@ -1327,7 +1332,7 @@ function baseCreateRenderer(
}
toggleRecurse(instance, true)

if (el && hydrateNode) {
if (el && hydrateNode && !isHmrReloading) {
Copy link
Member

@edison1105 edison1105 Oct 16, 2024

Choose a reason for hiding this comment

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

This is not the proper fix. if the initial VNode has el it means it needs hydration. so the proper fix should clear vnode.el before reload.

context.reload = () => {
// casting to ElementNamespace because TS doesn't guarantee type narrowing
// over function boundaries
render(
cloneVNode(vnode),
rootContainer,
namespace as ElementNamespace,
)
}
}

changes to

const cloned = cloneVNode(vnode)
cloned.el = null
render( 
  cloned, 
  rootContainer, 
  namespace as ElementNamespace, 
) 

BTW, this PR requires a test case.

// vnode has adopted host node - perform hydration instead of mount.
const hydrateSubTree = () => {
if (__DEV__) {
Expand Down