Skip to content

Commit d1f5d5d

Browse files
authored
fix: only inject push/pop/$$props in SSR components when necessary (#11771)
1 parent 9084f17 commit d1f5d5d

File tree

11 files changed

+31
-36
lines changed

11 files changed

+31
-36
lines changed

.changeset/smooth-pens-protect.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+
fix: only inject push/pop in SSR components when necessary

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2351,12 +2351,17 @@ export function server_component(analysis, options) {
23512351
if (options.dev) push_args.push(b.id(analysis.name));
23522352

23532353
const component_block = b.block([
2354-
b.stmt(b.call('$.push', ...push_args)),
23552354
.../** @type {import('estree').Statement[]} */ (instance.body),
2356-
.../** @type {import('estree').Statement[]} */ (template.body),
2357-
b.stmt(b.call('$.pop'))
2355+
.../** @type {import('estree').Statement[]} */ (template.body)
23582356
]);
23592357

2358+
let should_inject_context = analysis.needs_context || options.dev;
2359+
2360+
if (should_inject_context) {
2361+
component_block.body.unshift(b.stmt(b.call('$.push', ...push_args)));
2362+
component_block.body.push(b.stmt(b.call('$.pop')));
2363+
}
2364+
23602365
if (analysis.uses_rest_props) {
23612366
/** @type {string[]} */
23622367
const named_props = analysis.exports.map(({ name, alias }) => alias ?? name);
@@ -2388,9 +2393,18 @@ export function server_component(analysis, options) {
23882393

23892394
const body = [...state.hoisted, ...module.body];
23902395

2396+
let should_inject_props =
2397+
should_inject_context ||
2398+
props.length > 0 ||
2399+
analysis.needs_props ||
2400+
analysis.uses_props ||
2401+
analysis.uses_rest_props ||
2402+
analysis.uses_slots ||
2403+
analysis.slot_names.size > 0;
2404+
23912405
const component_function = b.function_declaration(
23922406
b.id(analysis.name),
2393-
[b.id('$$payload'), b.id('$$props')],
2407+
should_inject_props ? [b.id('$$payload'), b.id('$$props')] : [b.id('$$payload')],
23942408
component_block
23952409
);
23962410
if (options.legacy.componentApi) {

packages/svelte/tests/snapshot/samples/bind-component-snippet/_expected/server/index.svelte.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import * as $ from "svelte/internal/server";
22
import TextInput from './Child.svelte';
33

4-
export default function Bind_component_snippet($$payload, $$props) {
5-
$.push();
6-
4+
export default function Bind_component_snippet($$payload) {
75
let value = '';
86
const _snippet = snippet;
97

@@ -37,5 +35,4 @@ export default function Bind_component_snippet($$payload, $$props) {
3735
} while (!$$settled);
3836

3937
$.assign_payload($$payload, $$inner_payload);
40-
$.pop();
4138
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import * as $ from "svelte/internal/server";
22

3-
export default function Bind_this($$payload, $$props) {
4-
$.push();
3+
export default function Bind_this($$payload) {
54
$$payload.out += `<!--[-->`;
65
Foo($$payload, {});
76
$$payload.out += `<!--]-->`;
8-
$.pop();
97
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import * as $ from "svelte/internal/server";
22

3-
export default function Main($$payload, $$props) {
4-
$.push();
5-
3+
export default function Main($$payload) {
64
// needs to be a snapshot test because jsdom does auto-correct the attribute casing
75
let x = 'test';
86
let y = () => 'test';
97

108
$$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>`;
11-
$.pop();
129
}

packages/svelte/tests/snapshot/samples/each-string-template/_expected/server/index.svelte.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import * as $ from "svelte/internal/server";
22

3-
export default function Each_string_template($$payload, $$props) {
4-
$.push();
5-
3+
export default function Each_string_template($$payload) {
64
const each_array = $.ensure_array_like(['foo', 'bar', 'baz']);
75

86
$$payload.out += `<!--[-->`;
@@ -16,5 +14,4 @@ export default function Each_string_template($$payload, $$props) {
1614
}
1715

1816
$$payload.out += "<!--]-->";
19-
$.pop();
2017
}

packages/svelte/tests/snapshot/samples/function-prop-no-getter/_expected/server/index.svelte.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import * as $ from "svelte/internal/server";
22

3-
export default function Function_prop_no_getter($$payload, $$props) {
4-
$.push();
5-
3+
export default function Function_prop_no_getter($$payload) {
64
let count = 0;
75

86
function onmouseup() {
@@ -24,5 +22,4 @@ export default function Function_prop_no_getter($$payload, $$props) {
2422
});
2523

2624
$$payload.out += `<!--]-->`;
27-
$.pop();
2825
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import * as $ from "svelte/internal/server";
22

3-
export default function Hello_world($$payload, $$props) {
4-
$.push();
3+
export default function Hello_world($$payload) {
54
$$payload.out += `<h1>hello world</h1>`;
6-
$.pop();
75
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import * as $ from "svelte/internal/server";
22

3-
export default function Hmr($$payload, $$props) {
4-
$.push();
3+
export default function Hmr($$payload) {
54
$$payload.out += `<h1>hello world</h1>`;
6-
$.pop();
75
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import * as $ from "svelte/internal/server";
22

3-
export default function State_proxy_literal($$payload, $$props) {
4-
$.push();
5-
3+
export default function State_proxy_literal($$payload) {
64
let str = '';
75
let tpl = ``;
86

@@ -14,5 +12,4 @@ export default function State_proxy_literal($$payload, $$props) {
1412
}
1513

1614
$$payload.out += `<input${$.attr("value", str, false)}> <input${$.attr("value", tpl, false)}> <button>reset</button>`;
17-
$.pop();
1815
}
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import * as $ from "svelte/internal/server";
22

33
export default function Svelte_element($$payload, $$props) {
4-
$.push();
5-
64
let { tag = 'hr' } = $$props;
75

86
$$payload.out += `<!--[-->`;
97
if (tag) $.element($$payload, tag, () => {}, () => {});
108
$$payload.out += `<!--]-->`;
11-
$.pop();
129
}

0 commit comments

Comments
 (0)