Skip to content

Commit 9e2c363

Browse files
committed
Use replaceWith and dispatch a custom event with fragment
1 parent 8ac2a25 commit 9e2c363

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ async function handleData(el: IncludeFragmentElement) {
1010
// eslint-disable-next-line github/no-then
1111
return getData(el).then(
1212
function (html: string) {
13-
const parentNode = el.parentNode
14-
if (parentNode) {
15-
el.insertAdjacentHTML('afterend', html)
16-
parentNode.removeChild(el)
17-
}
13+
const template = document.createElement('template')
14+
template.innerHTML = html
15+
const fragment = document.importNode(template.content, true)
16+
const canceled = !el.dispatchEvent(new CustomEvent('include-fragment-replace', {cancelable: true, detail: {fragment}}))
17+
if (canceled) return
18+
el.replaceWith(fragment)
19+
el.dispatchEvent(new CustomEvent('include-fragment-replaced'))
1820
},
1921
function () {
2022
el.classList.add('is-error')

0 commit comments

Comments
 (0)