Skip to content

Commit 1bf68d8

Browse files
committed
simplify
1 parent 5516a2f commit 1bf68d8

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { HYDRATION_END, HYDRATION_ERROR, HYDRATION_START } from '../../../../con
77
import { hydration_mismatch } from '../../warnings.js';
88

99
/**
10-
* @type {Node | undefined}
10+
* @type {Node | null | undefined}
1111
*/
1212
let head_anchor;
1313

@@ -33,15 +33,15 @@ export function head(render_fn) {
3333

3434
// There might be multiple head blocks in our app, so we need to account for each one needing independent hydration.
3535
if (head_anchor === undefined) {
36-
head_anchor = /** @type {TemplateNode} */ (get_first_child(document.head));
36+
head_anchor = get_first_child(document.head);
3737
}
3838

3939
while (
4040
head_anchor !== null &&
4141
(head_anchor.nodeType !== COMMENT_NODE ||
4242
/** @type {Comment} */ (head_anchor).data !== HYDRATION_START)
4343
) {
44-
head_anchor = /** @type {TemplateNode} */ (get_next_sibling(head_anchor));
44+
head_anchor = get_next_sibling(head_anchor);
4545
}
4646

4747
// If we can't find an opening hydration marker, skip hydration (this can happen
@@ -61,26 +61,28 @@ export function head(render_fn) {
6161
block(() => render_fn(anchor), HEAD_EFFECT);
6262
check_end();
6363
} catch (error) {
64-
// re-mount only this svelte:head
64+
// Remount only this svelte:head
6565
if (was_hydrating && head_anchor && error === HYDRATION_ERROR) {
6666
// Here head_anchor is the node next after HYDRATION_START
6767
/** @type {Node | null} */
68-
let prev = head_anchor.previousSibling;
69-
/** @type {Node | null} */
70-
let next = head_anchor;
71-
// remove nodes that failed to hydrate
72-
while (
73-
prev !== null &&
74-
(prev.nodeType !== COMMENT_NODE || /** @type {Comment} */ (prev).data !== HYDRATION_END)
75-
) {
76-
document.head.removeChild(prev);
77-
prev = next;
78-
next = get_next_sibling(/** @type {Node} */ (next));
68+
let node = head_anchor.previousSibling;
69+
// Remove nodes that failed to hydrate
70+
while (node !== null) {
71+
const removed = node;
72+
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;
79+
}
7980
}
80-
if (prev?.parentNode) document.head.removeChild(prev);
81-
if (next !== null) {
82-
// allow the next head block try to hydrate
83-
head_anchor = set_hydrate_node(/** @type {TemplateNode} */ (next));
81+
// Setup hydration for the next svelte:head
82+
if (node === null) {
83+
head_anchor = null;
84+
} else {
85+
head_anchor = set_hydrate_node(/** @type {TemplateNode} */ (node));
8486
}
8587

8688
set_hydrating(false);

0 commit comments

Comments
 (0)