Skip to content

Commit a92e715

Browse files
committed
fix: hydrate multiple <svelte:head> elements correctly
The head hydration anchor didn't update after hydrating the contents of one `<svelte:head>` element, which meant subsequent `<svelte:head>` elements would always start at the beginning of the head. This PR fixes that. The test was updated such that the shape of each `<svelte:head>` content is sufficiently different to throw an error if this wasn't fixed. fixes #12458
1 parent c287bd5 commit a92e715

File tree

5 files changed

+19
-6
lines changed

5 files changed

+19
-6
lines changed

.changeset/clever-toys-laugh.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: hydrate multiple `<svelte:head>` elements correctly

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export function head(render_fn) {
5151
block(() => render_fn(anchor), HEAD_EFFECT);
5252
} finally {
5353
if (was_hydrating) {
54+
head_anchor = hydrate_node; // so that next head block starts from the correct node
5455
set_hydrate_node(/** @type {TemplateNode} */ (previous_hydrate_node));
5556
}
5657
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
{@html '<meta name="head_nested_html" content="head_nested_html">'}
2-
<meta name="head_nested" content="head_nested">
2+
<meta name="head_nested" content="head_nested" />
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
<script>
2+
let text = $state('foo');
3+
</script>
14

25
<svelte:head>
36
{@html '<meta name="nested_html" content="nested_html">'}
4-
<meta name="nested" content="nested">
7+
<meta name="nested" content="nested" />
8+
<meta name="foo" content={text} />
59
</svelte:head>

packages/svelte/tests/hydration/samples/head-html-and-component/main.svelte

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
</script>
55

66
<svelte:head>
7-
{@html '<meta name="main_html" content="main_html">'}
8-
<meta name="main" content="main">
9-
<HeadNested />
7+
<!-- the if block forces a comment node; tests that the nested head starts at the correct node -->
8+
{#if true}
9+
{@html '<meta name="main_html" content="main_html">'}
10+
<meta name="main" content="main" />
11+
<HeadNested />
12+
{/if}
1013
</svelte:head>
1114

12-
<Nested/>
15+
<Nested />

0 commit comments

Comments
 (0)