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): hosited node hmr should look up correct el #1628

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 6 additions & 3 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export interface RendererOptions<
anchor: HostNode | null,
isSVG: boolean
): HostElement[]
getEqualNodeFromContainer?(container: HostElement, node: HostNode): HostNode
}

// Renderer Node can technically be any object in the context of core renderer
Expand Down Expand Up @@ -398,7 +399,8 @@ function baseCreateRenderer(
nextSibling: hostNextSibling,
setScopeId: hostSetScopeId = NOOP,
cloneNode: hostCloneNode,
insertStaticContent: hostInsertStaticContent
insertStaticContent: hostInsertStaticContent,
getEqualNodeFromContainer: hostGetEqualNodeFromContainer
} = options

// Note: functions inside this closure should use `const xxx = () => {}`
Expand Down Expand Up @@ -651,16 +653,17 @@ function baseCreateRenderer(
// never patched (because they are not collected as dynamic nodes), but
// they can be udpated during HMR. In this case just mount it as new
// and remove the stale DOM tree.
const el = hostGetEqualNodeFromContainer!(container, n1.el!)
mountElement(
n2,
container,
n1.el,
el,
parentComponent,
parentSuspense,
isSVG,
optimized
)
hostRemove(n1.el!)
hostRemove(el)
} else {
patchElement(n1, n2, parentComponent, parentSuspense, isSVG, optimized)
}
Expand Down
12 changes: 12 additions & 0 deletions packages/runtime-dom/src/nodeOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,17 @@ export const nodeOps: Omit<RendererOptions<Node, Element>, 'patchProp'> = {
node = temp.firstChild as Element
}
return [first, last]
},

getEqualNodeFromContainer(container, node) {
const parentNode = node.parentNode
if (parentNode === container) {
return node
} else {
const index = Array.prototype.slice
.call(parentNode!.children)
.indexOf(node)
return container.children[index]
}
}
}