Skip to content

Commit b605d4a

Browse files
committed
fix: enhance chunk rendering logic to support outerHTML insertion
1 parent 55ba3a2 commit b605d4a

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/index.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,24 @@ class CoCreateServerSideRender {
158158
let chunkDom = parse(chunk);
159159
chunkDom = await render(chunkDom);
160160

161-
el.setAttribute("rendered", "");
162-
el.innerHTML = "";
163-
// Append all child nodes of chunkDom to el
164-
for (const child of chunkDom.childNodes) {
165-
el.appendChild(child);
161+
// If element requests outerHTML insertion, replace the element
162+
// with the fetched chunk's content. Otherwise append children.
163+
const valueType = el.getAttribute && el.getAttribute("value-type");
164+
if (valueType === "outerHTML") {
165+
// Replace the element with the parsed chunk's child nodes
166+
// spread child nodes so we don't create an extra wrapper
167+
if (chunkDom.childNodes && chunkDom.childNodes.length) {
168+
el.replaceWith(...chunkDom.childNodes);
169+
} else {
170+
// If no child nodes, just remove the element
171+
el.remove();
172+
}
173+
} else {
174+
el.setAttribute("rendered", "");
175+
el.innerHTML = "";
176+
for (const child of chunkDom.childNodes) {
177+
el.appendChild(child);
178+
}
166179
}
167180
}
168181
}

0 commit comments

Comments
 (0)