Skip to content

Commit 565ae54

Browse files
committed
check for any error and better node removal
1 parent 62bf167 commit 565ae54

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

packages/svelte/src/internal/client/dom/blocks/svelte-head.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,25 @@ export function head(render_fn) {
6262
check_end();
6363
} catch (error) {
6464
// Remount only this svelte:head
65-
if (was_hydrating && head_anchor && error === HYDRATION_ERROR) {
65+
if (was_hydrating && head_anchor != null) {
66+
hydration_mismatch();
6667
// Here head_anchor is the node next after HYDRATION_START
6768
/** @type {Node | null} */
68-
let node = head_anchor.previousSibling;
69+
var node = head_anchor.previousSibling;
6970
// Remove nodes that failed to hydrate
71+
var depth = 0;
7072
while (node !== null) {
71-
const removed = node;
73+
var prev = /** @type {TemplateNode} */ (node);
7274
node = get_next_sibling(node);
73-
document.head.removeChild(removed);
74-
if (
75-
removed.nodeType === COMMENT_NODE &&
76-
/** @type {Comment} */ (removed).data === HYDRATION_END
77-
) {
78-
break;
75+
prev.remove();
76+
if (prev.nodeType === COMMENT_NODE) {
77+
var data = /** @type {Comment} */ (prev).data;
78+
if (data === HYDRATION_END) {
79+
depth -= 1;
80+
if (depth === 0) break;
81+
} else if (data === HYDRATION_START) {
82+
depth += 1;
83+
}
7984
}
8085
}
8186
// Setup hydration for the next svelte:head

0 commit comments

Comments
 (0)