Skip to content

Commit bbf4895

Browse files
authored
feat: better <svelte:element> SSR output (#12339)
1 parent a59bc99 commit bbf4895

File tree

4 files changed

+30
-16
lines changed

4 files changed

+30
-16
lines changed

.changeset/plenty-clouds-reply.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+
feat: better `<svelte:element>` SSR output

packages/svelte/src/compiler/phases/3-transform/server/transform-server.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,12 +1338,18 @@ const template_visitors = {
13381338
context.visit(node.fragment, state)
13391339
);
13401340

1341-
const body = b.stmt(
1342-
b.call('$.element', b.id('$$payload'), tag, b.thunk(attributes), b.thunk(children))
1341+
context.state.template.push(
1342+
b.stmt(
1343+
b.call(
1344+
'$.element',
1345+
b.id('$$payload'),
1346+
tag,
1347+
attributes.body.length > 0 && b.thunk(attributes),
1348+
children.body.length > 0 && b.thunk(children)
1349+
)
1350+
)
13431351
);
13441352

1345-
context.state.template.push(b.if(tag, body), block_anchor);
1346-
13471353
if (context.state.options.dev) {
13481354
context.state.template.push(b.stmt(b.call('$.pop_element')));
13491355
}

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,22 @@ export function assign_payload(p1, p2) {
7272
* @param {() => void} children_fn
7373
* @returns {void}
7474
*/
75-
export function element(payload, tag, attributes_fn, children_fn) {
76-
payload.out += `<${tag} `;
77-
attributes_fn();
78-
payload.out += `>`;
79-
80-
if (!VoidElements.has(tag)) {
81-
children_fn();
82-
if (!RawTextElements.includes(tag)) {
83-
payload.out += BLOCK_ANCHOR;
75+
export function element(payload, tag, attributes_fn = noop, children_fn = noop) {
76+
if (tag) {
77+
payload.out += `<${tag} `;
78+
attributes_fn();
79+
payload.out += `>`;
80+
81+
if (!VoidElements.has(tag)) {
82+
children_fn();
83+
if (!RawTextElements.includes(tag)) {
84+
payload.out += BLOCK_ANCHOR;
85+
}
86+
payload.out += `</${tag}>`;
8487
}
85-
payload.out += `</${tag}>`;
8688
}
89+
90+
payload.out += '<!---->';
8791
}
8892

8993
/**

packages/svelte/tests/snapshot/samples/svelte-element/_expected/server/index.svelte.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ import * as $ from "svelte/internal/server";
33
export default function Svelte_element($$payload, $$props) {
44
let { tag = 'hr' } = $$props;
55

6-
if (tag) $.element($$payload, tag, () => {}, () => {});
7-
$$payload.out += `<!---->`;
6+
$.element($$payload, tag);
87
}

0 commit comments

Comments
 (0)