From ff8dff993d31cf5990697d6bb3dba9c27265b47d Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Wed, 10 Jan 2024 17:18:38 +0000 Subject: [PATCH] fix: improve ssr template code generation (#10151) * fix: improve ssr template code generation * lint --- .changeset/breezy-carrots-flash.md | 5 +++++ .../phases/3-transform/server/transform-server.js | 10 ++++++---- .../_expected/server/index.svelte.js | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) create mode 100644 .changeset/breezy-carrots-flash.md diff --git a/.changeset/breezy-carrots-flash.md b/.changeset/breezy-carrots-flash.md new file mode 100644 index 0000000000..8d9fce63f9 --- /dev/null +++ b/.changeset/breezy-carrots-flash.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: improve ssr template code generation diff --git a/packages/svelte/src/compiler/phases/3-transform/server/transform-server.js b/packages/svelte/src/compiler/phases/3-transform/server/transform-server.js index 5cb479b91a..d097e68676 100644 --- a/packages/svelte/src/compiler/phases/3-transform/server/transform-server.js +++ b/packages/svelte/src/compiler/phases/3-transform/server/transform-server.js @@ -187,10 +187,12 @@ function process_children(nodes, parent, { visit, state }) { const node = sequence[i]; if (node.type === 'Text' || node.type === 'Comment') { let last = /** @type {import('estree').TemplateElement} */ (quasis.at(-1)); - last.value.raw += - node.type === 'Comment' - ? `` - : sanitize_template_string(escape_html(node.data)); + last.value.raw += node.type === 'Comment' ? `` : escape_html(node.data); + } else if (node.type === 'ExpressionTag' && node.expression.type === 'Literal') { + let last = /** @type {import('estree').TemplateElement} */ (quasis.at(-1)); + if (node.expression.value != null) { + last.value.raw += escape_html(node.expression.value + ''); + } } else if (node.type === 'Anchor') { expressions.push(node.id); quasis.push(b.quasi('', i + 1 === sequence.length)); diff --git a/packages/svelte/tests/snapshot/samples/each-string-template/_expected/server/index.svelte.js b/packages/svelte/tests/snapshot/samples/each-string-template/_expected/server/index.svelte.js index a4bbf5a982..b23b316e3d 100644 --- a/packages/svelte/tests/snapshot/samples/each-string-template/_expected/server/index.svelte.js +++ b/packages/svelte/tests/snapshot/samples/each-string-template/_expected/server/index.svelte.js @@ -14,7 +14,7 @@ export default function Each_string_template($$payload, $$props) { const thing = each_array[$$index]; const anchor_1 = $.create_anchor($$payload); - $$payload.out += `${anchor_1}${$.escape(thing)},${$.escape(' ')}${anchor_1}`; + $$payload.out += `${anchor_1}${$.escape(thing)}, ${anchor_1}`; } $$payload.out += `${anchor}`;