Skip to content

Commit 6e143c1

Browse files
committed
fix: better handle hydration of script/style elements
1 parent cb5734a commit 6e143c1

File tree

5 files changed

+28
-2
lines changed

5 files changed

+28
-2
lines changed

.changeset/rotten-yaks-nail.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: better handle hydration of script/style elements

packages/svelte/src/internal/client/dom/hydration.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,16 @@ export function set_hydrate_node(node) {
4040
}
4141

4242
export function hydrate_next() {
43-
return set_hydrate_node(/** @type {TemplateNode} */ (get_next_sibling(hydrate_node)));
43+
var sibling = /** @type {TemplateNode} */ (get_next_sibling(hydrate_node));
44+
if (sibling === null) {
45+
var parent_node_name = hydrate_node.parentNode?.nodeName;
46+
// If we're hydrating inside a <script> or <style> element then there is no
47+
// closing anchor
48+
if (parent_node_name === 'SCRIPT' || parent_node_name === 'STYLE') {
49+
return;
50+
}
51+
}
52+
return set_hydrate_node(sibling);
4453
}
4554

4655
/** @param {TemplateNode} node */

packages/svelte/src/internal/server/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export function element(payload, tag, attributes_fn = noop, children_fn = noop)
6464
payload.out += '<!---->';
6565

6666
if (tag) {
67-
payload.out += `<${tag} `;
67+
payload.out += `<${tag}`;
6868
attributes_fn();
6969
payload.out += `>`;
7070

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
snapshot(target) {
5+
const script = target.querySelector('script');
6+
7+
return {
8+
script
9+
};
10+
}
11+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<svelte:element this={"script"}>{"{}"}</svelte:element>

0 commit comments

Comments
 (0)