Skip to content

Commit

Permalink
fix(hmr): support hmr for static nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Apr 30, 2020
1 parent 9f8ed4a commit 386b093
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export interface RendererOptions<
anchor: HostNode | null,
isSVG: boolean
): HostElement
setStaticContent?(node: HostElement, content: string): void
}

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

// Note: functions inside this closure should use `const xxx = () => {}`
Expand Down Expand Up @@ -363,7 +365,13 @@ function baseCreateRenderer(
case Static:
if (n1 == null) {
mountStaticNode(n2, container, anchor, isSVG)
} // static nodes are noop on patch
} else if (__DEV__) {
// static nodes are only patched during dev for HMR
n2.el = n1.el
if (n2.children !== n1.children) {
hostSetStaticContent!(n2.el!, n2.children as string)
}
}
break
case Fragment:
processFragment(
Expand Down
9 changes: 9 additions & 0 deletions packages/runtime-dom/src/nodeOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,12 @@ export const nodeOps: Omit<RendererOptions<Node, Element>, 'patchProp'> = {
return node
}
}

if (__DEV__) {
// __UNSAFE__
// Reason: innerHTML.
// same as `insertStaticContent`, but this is also dev only (for HMR).
nodeOps.setStaticContent = (el, content) => {
el.innerHTML = content
}
}

0 comments on commit 386b093

Please sign in to comment.