Skip to content

Commit 4f12846

Browse files
authored
feat: more efficient output for attributes in SSR (#11949)
* feat: more efficient output for attributes in SSR * rename arg
1 parent 968bba5 commit 4f12846

File tree

5 files changed

+12
-7
lines changed

5 files changed

+12
-7
lines changed

.changeset/spotty-crabs-give.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: more efficient output for attributes in SSR

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2059,7 +2059,7 @@ function serialize_element_attributes(node, context) {
20592059
);
20602060

20612061
context.state.template.push(
2062-
t_expression(b.call('$.attr', b.literal(name), value, b.literal(is_boolean)))
2062+
t_expression(b.call('$.attr', b.literal(name), value, is_boolean && b.literal(is_boolean)))
20632063
);
20642064
}
20652065
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,12 @@ export function head(payload, fn) {
143143
* @template V
144144
* @param {string} name
145145
* @param {V} value
146-
* @param {boolean} boolean
146+
* @param {boolean} [is_boolean]
147147
* @returns {string}
148148
*/
149-
export function attr(name, value, boolean) {
150-
if (value == null || (!value && boolean) || (value === '' && name === 'class')) return '';
151-
const assignment = boolean ? '' : `="${escape_html(value, true)}"`;
149+
export function attr(name, value, is_boolean = false) {
150+
if (value == null || (!value && is_boolean) || (value === '' && name === 'class')) return '';
151+
const assignment = is_boolean ? '' : `="${escape_html(value, true)}"`;
152152
return ` ${name}${assignment}`;
153153
}
154154

packages/svelte/tests/snapshot/samples/dynamic-attributes-casing/_expected/server/main.svelte.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ export default function Main($$payload) {
55
let x = 'test';
66
let y = () => 'test';
77

8-
$$payload.out += `<div${$.attr("foobar", x, false)}></div> <svg${$.attr("viewBox", x, false)}></svg> <custom-element${$.attr("foobar", x, false)}></custom-element> <div${$.attr("foobar", y(), false)}></div> <svg${$.attr("viewBox", y(), false)}></svg> <custom-element${$.attr("foobar", y(), false)}></custom-element>`;
8+
$$payload.out += `<div${$.attr("foobar", x)}></div> <svg${$.attr("viewBox", x)}></svg> <custom-element${$.attr("foobar", x)}></custom-element> <div${$.attr("foobar", y())}></div> <svg${$.attr("viewBox", y())}></svg> <custom-element${$.attr("foobar", y())}></custom-element>`;
99
}

packages/svelte/tests/snapshot/samples/state-proxy-literal/_expected/server/index.svelte.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ export default function State_proxy_literal($$payload) {
1111
tpl = ``;
1212
}
1313

14-
$$payload.out += `<input${$.attr("value", str, false)}> <input${$.attr("value", tpl, false)}> <button>reset</button>`;
14+
$$payload.out += `<input${$.attr("value", str)}> <input${$.attr("value", tpl)}> <button>reset</button>`;
1515
}

0 commit comments

Comments
 (0)