diff --git a/.changeset/khaki-tomatoes-rule.md b/.changeset/khaki-tomatoes-rule.md new file mode 100644 index 0000000000..b156f9bf54 --- /dev/null +++ b/.changeset/khaki-tomatoes-rule.md @@ -0,0 +1,5 @@ +--- +"svelte": patch +--- + +fix: ssr comments in head elements that require raw content diff --git a/packages/svelte/src/constants.js b/packages/svelte/src/constants.js index 159cbfb7b6..7755872ba5 100644 --- a/packages/svelte/src/constants.js +++ b/packages/svelte/src/constants.js @@ -25,6 +25,9 @@ export const HYDRATION_END_ELSE = `${HYDRATION_END}!`; // used to indicate that export const UNINITIALIZED = Symbol(); +/** List of elements that require raw contents and should not have SSR comments put in them */ +export const RawTextElements = ['textarea', 'script', 'style', 'title']; + /** List of Element events that will be delegated */ export const DelegatedEvents = [ 'beforeinput', diff --git a/packages/svelte/src/internal/server/index.js b/packages/svelte/src/internal/server/index.js index 6a5e8aa177..6c3263cd1c 100644 --- a/packages/svelte/src/internal/server/index.js +++ b/packages/svelte/src/internal/server/index.js @@ -3,6 +3,7 @@ import { subscribe_to_store } from '../../store/utils.js'; import { UNINITIALIZED, DOMBooleanAttributes, + RawTextElements, disallowed_paragraph_contents, interactive_elements, is_tag_valid_with_parent @@ -161,11 +162,11 @@ export function element(payload, tag, attributes_fn, children_fn) { payload.out += `>`; if (!VoidElements.has(tag)) { - if (tag !== 'textarea') { + if (!RawTextElements.includes(tag)) { payload.out += BLOCK_OPEN; } children_fn(); - if (tag !== 'textarea') { + if (!RawTextElements.includes(tag)) { payload.out += BLOCK_CLOSE; } payload.out += `${tag}>`; diff --git a/packages/svelte/tests/server-side-rendering/samples/head-svelte-components-raw-content/_config.js b/packages/svelte/tests/server-side-rendering/samples/head-svelte-components-raw-content/_config.js new file mode 100644 index 0000000000..7634075129 --- /dev/null +++ b/packages/svelte/tests/server-side-rendering/samples/head-svelte-components-raw-content/_config.js @@ -0,0 +1,7 @@ +import { test } from '../../test'; + +export default test({ + compileOptions: { + preserveComments: true + } +}); diff --git a/packages/svelte/tests/server-side-rendering/samples/head-svelte-components-raw-content/_expected.html b/packages/svelte/tests/server-side-rendering/samples/head-svelte-components-raw-content/_expected.html new file mode 100644 index 0000000000..3eca221c36 --- /dev/null +++ b/packages/svelte/tests/server-side-rendering/samples/head-svelte-components-raw-content/_expected.html @@ -0,0 +1,16 @@ + + +