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): cleanup hoisted vnode when unmount component #9219

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,11 @@ export interface ComponentInternalInstance {
* @internal
*/
ut?: (vars?: Record<string, string>) => void
/**
* cleanup el of hoisted vnode
* @internal
*/
hoistedCleanup?: (() => void)[] | null
edison1105 marked this conversation as resolved.
Show resolved Hide resolved
}

const emptyAppContext = createAppContext()
Expand Down Expand Up @@ -511,6 +516,7 @@ export function createComponentInstance(
provides: parent ? parent.provides : Object.create(appContext.provides),
accessCache: null!,
renderCache: [],
hoistedCleanup: null,

// local resolved assets
components: null,
Expand Down
14 changes: 13 additions & 1 deletion packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ function baseCreateRenderer(
) => {
let el: RendererElement
let vnodeHook: VNodeHook | undefined | null
const { type, props, shapeFlag, transition, dirs } = vnode
const { type, props, shapeFlag, patchFlag, transition, dirs } = vnode

el = vnode.el = hostCreateElement(
vnode.type as string,
Expand All @@ -630,6 +630,14 @@ function baseCreateRenderer(
props
)

if (patchFlag === PatchFlags.HOISTED && parentComponent) {
;(
parentComponent.hoistedCleanup || (parentComponent.hoistedCleanup = [])
).push(() => {
vnode.el = null
})
}

// mount children first, since some props may rely on child content
// being already rendered, e.g. `<select value>`
if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
Expand Down Expand Up @@ -2268,6 +2276,10 @@ function baseCreateRenderer(
)
}
queuePostRenderEffect(() => {
if (instance.hoistedCleanup) {
instance.hoistedCleanup.forEach(cleanup => cleanup())
instance.hoistedCleanup = null
}
instance.isUnmounted = true
}, parentSuspense)

Expand Down