feat: more efficient output for attributes in SSR (#11949)

* feat: more efficient output for attributes in SSR

* rename arg
pull/11940/head
Rich Harris 1 year ago committed by GitHub
parent 968bba5e54
commit 4f12846fdf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
feat: more efficient output for attributes in SSR

@ -2059,7 +2059,7 @@ function serialize_element_attributes(node, context) {
); );
context.state.template.push( context.state.template.push(
t_expression(b.call('$.attr', b.literal(name), value, b.literal(is_boolean))) t_expression(b.call('$.attr', b.literal(name), value, is_boolean && b.literal(is_boolean)))
); );
} }
} }

@ -143,12 +143,12 @@ export function head(payload, fn) {
* @template V * @template V
* @param {string} name * @param {string} name
* @param {V} value * @param {V} value
* @param {boolean} boolean * @param {boolean} [is_boolean]
* @returns {string} * @returns {string}
*/ */
export function attr(name, value, boolean) { export function attr(name, value, is_boolean = false) {
if (value == null || (!value && boolean) || (value === '' && name === 'class')) return ''; if (value == null || (!value && is_boolean) || (value === '' && name === 'class')) return '';
const assignment = boolean ? '' : `="${escape_html(value, true)}"`; const assignment = is_boolean ? '' : `="${escape_html(value, true)}"`;
return ` ${name}${assignment}`; return ` ${name}${assignment}`;
} }

@ -5,5 +5,5 @@ export default function Main($$payload) {
let x = 'test'; let x = 'test';
let y = () => 'test'; let y = () => 'test';
$$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>`; $$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>`;
} }

@ -11,5 +11,5 @@ export default function State_proxy_literal($$payload) {
tpl = ``; tpl = ``;
} }
$$payload.out += `<input${$.attr("value", str, false)}> <input${$.attr("value", tpl, false)}> <button>reset</button>`; $$payload.out += `<input${$.attr("value", str)}> <input${$.attr("value", tpl)}> <button>reset</button>`;
} }
Loading…
Cancel
Save