Skip to content

Commit d061f2f

Browse files
authored
fix: ssr comments in head elements that require raw content (#10936)
* added raw elements set * added test * added changeset * moved raw text elements to constands and made array * moved to correct constants * fix test * fix constants formatting
1 parent 322737a commit d061f2f

File tree

6 files changed

+37
-2
lines changed

6 files changed

+37
-2
lines changed

.changeset/khaki-tomatoes-rule.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: ssr comments in head elements that require raw content

packages/svelte/src/constants.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ export const HYDRATION_END_ELSE = `${HYDRATION_END}!`; // used to indicate that
2525

2626
export const UNINITIALIZED = Symbol();
2727

28+
/** List of elements that require raw contents and should not have SSR comments put in them */
29+
export const RawTextElements = ['textarea', 'script', 'style', 'title'];
30+
2831
/** List of Element events that will be delegated */
2932
export const DelegatedEvents = [
3033
'beforeinput',

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { subscribe_to_store } from '../../store/utils.js';
33
import {
44
UNINITIALIZED,
55
DOMBooleanAttributes,
6+
RawTextElements,
67
disallowed_paragraph_contents,
78
interactive_elements,
89
is_tag_valid_with_parent
@@ -161,11 +162,11 @@ export function element(payload, tag, attributes_fn, children_fn) {
161162
payload.out += `>`;
162163

163164
if (!VoidElements.has(tag)) {
164-
if (tag !== 'textarea') {
165+
if (!RawTextElements.includes(tag)) {
165166
payload.out += BLOCK_OPEN;
166167
}
167168
children_fn();
168-
if (tag !== 'textarea') {
169+
if (!RawTextElements.includes(tag)) {
169170
payload.out += BLOCK_CLOSE;
170171
}
171172
payload.out += `</${tag}>`;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
compileOptions: {
5+
preserveComments: true
6+
}
7+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!--[-->
2+
<!--[-->
3+
<title>lorem</title>
4+
<!--]-->
5+
<!--[-->
6+
<style>
7+
.ipsum {
8+
display: block;
9+
}
10+
</style>
11+
<!--]-->
12+
<!--[-->
13+
<script>
14+
console.log(true);
15+
</script>
16+
<!--]--><!--]-->
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<svelte:element this="title">lorem</svelte:element>
2+
<svelte:element this="style">{'.ipsum { display: block; }'}</svelte:element>
3+
<svelte:element this="script">{'console.log(true);'}</svelte:element>

0 commit comments

Comments
 (0)