vue3-core/packages/runtime-vapor/src/template.ts

21 lines
711 B
TypeScript

export const template = (str: string): (() => Node) => {
let cached = false
let node: Node
return () => {
if (!cached) {
cached = true
const t = document.createElement('template')
t.innerHTML = str
// first render: insert the node directly.
// this removes it from the template fragment to avoid keeping two copies
// of the inserted tree in memory, even if the template is used only once.
return (node = t.content.firstChild!)
} else {
// repeated renders: clone from cache. This is more performant and
// efficient when dealing with big lists where the template is repeated
// many times.
return node.cloneNode(true)
}
}
}