Skip to content

Commit 386b093

Browse files
committed
fix(hmr): support hmr for static nodes
1 parent 9f8ed4a commit 386b093

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

packages/runtime-core/src/renderer.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export interface RendererOptions<
115115
anchor: HostNode | null,
116116
isSVG: boolean
117117
): HostElement
118+
setStaticContent?(node: HostElement, content: string): void
118119
}
119120

120121
// Renderer Node can technically be any object in the context of core renderer
@@ -330,7 +331,8 @@ function baseCreateRenderer(
330331
nextSibling: hostNextSibling,
331332
setScopeId: hostSetScopeId = NOOP,
332333
cloneNode: hostCloneNode,
333-
insertStaticContent: hostInsertStaticContent
334+
insertStaticContent: hostInsertStaticContent,
335+
setStaticContent: hostSetStaticContent
334336
} = options
335337

336338
// Note: functions inside this closure should use `const xxx = () => {}`
@@ -363,7 +365,13 @@ function baseCreateRenderer(
363365
case Static:
364366
if (n1 == null) {
365367
mountStaticNode(n2, container, anchor, isSVG)
366-
} // static nodes are noop on patch
368+
} else if (__DEV__) {
369+
// static nodes are only patched during dev for HMR
370+
n2.el = n1.el
371+
if (n2.children !== n1.children) {
372+
hostSetStaticContent!(n2.el!, n2.children as string)
373+
}
374+
}
367375
break
368376
case Fragment:
369377
processFragment(

packages/runtime-dom/src/nodeOps.ts

+9
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,12 @@ export const nodeOps: Omit<RendererOptions<Node, Element>, 'patchProp'> = {
6969
return node
7070
}
7171
}
72+
73+
if (__DEV__) {
74+
// __UNSAFE__
75+
// Reason: innerHTML.
76+
// same as `insertStaticContent`, but this is also dev only (for HMR).
77+
nodeOps.setStaticContent = (el, content) => {
78+
el.innerHTML = content
79+
}
80+
}

0 commit comments

Comments
 (0)