diff --git a/.changeset/rotten-experts-relax.md b/.changeset/rotten-experts-relax.md
new file mode 100644
index 0000000000..e5a7e3bbe4
--- /dev/null
+++ b/.changeset/rotten-experts-relax.md
@@ -0,0 +1,5 @@
+---
+"svelte": patch
+---
+
+fix: permit whitespace within template scripts
diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js
index fd48fda1b7..1b95031bb6 100644
--- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js
+++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js
@@ -2081,7 +2081,7 @@ export const template_visitors = {
 			node.fragment.nodes,
 			context.path,
 			child_metadata.namespace,
-			state.preserve_whitespace,
+			node.name === 'script' || state.preserve_whitespace,
 			state.options.preserveComments
 		);
 
diff --git a/packages/svelte/tests/runtime-legacy/samples/head-script/_config.js b/packages/svelte/tests/runtime-legacy/samples/head-script/_config.js
new file mode 100644
index 0000000000..31acab66a3
--- /dev/null
+++ b/packages/svelte/tests/runtime-legacy/samples/head-script/_config.js
@@ -0,0 +1,8 @@
+import { test } from '../../test';
+
+export default test({
+	test({ assert, component, window }) {
+		document.dispatchEvent(new Event('DOMContentLoaded'));
+		assert.equal(window.document.querySelector('button')?.textContent, 'Hello world');
+	}
+});
diff --git a/packages/svelte/tests/runtime-legacy/samples/head-script/main.svelte b/packages/svelte/tests/runtime-legacy/samples/head-script/main.svelte
new file mode 100644
index 0000000000..8284f84a59
--- /dev/null
+++ b/packages/svelte/tests/runtime-legacy/samples/head-script/main.svelte
@@ -0,0 +1,10 @@
+<svelte:head>
+	<script>
+		// A comment
+		const val = 'Hello world';
+		document.addEventListener('DOMContentLoaded', () => {
+			document.querySelector('button').textContent = val;
+		});
+	</script>
+</svelte:head>
+<button />