Skip to content

Commit c5fd24b

Browse files
committed
fix: only inject push/pop in SSR components when necessary
1 parent 9084f17 commit c5fd24b

File tree

10 files changed

+6
-27
lines changed

10 files changed

+6
-27
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2351,12 +2351,15 @@ 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+
if (analysis.needs_context || options.dev) {
2359+
component_block.body.unshift(b.stmt(b.call('$.push', ...push_args)));
2360+
component_block.body.push(b.stmt(b.call('$.pop')));
2361+
}
2362+
23602363
if (analysis.uses_rest_props) {
23612364
/** @type {string[]} */
23622365
const named_props = analysis.exports.map(({ name, alias }) => alias ?? name);

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import * as $ from "svelte/internal/server";
22
import TextInput from './Child.svelte';
33

44
export default function Bind_component_snippet($$payload, $$props) {
5-
$.push();
6-
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: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import * as $ from "svelte/internal/server";
22

33
export default function Bind_this($$payload, $$props) {
4-
$.push();
54
$$payload.out += `<!--[-->`;
65
Foo($$payload, {});
76
$$payload.out += `<!--]-->`;
8-
$.pop();
97
}
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 Main($$payload, $$props) {
4-
$.push();
5-
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: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import * as $ from "svelte/internal/server";
22

33
export default function Each_string_template($$payload, $$props) {
4-
$.push();
5-
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: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import * as $ from "svelte/internal/server";
22

33
export default function Function_prop_no_getter($$payload, $$props) {
4-
$.push();
5-
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: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import * as $ from "svelte/internal/server";
22

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

33
export default function Hmr($$payload, $$props) {
4-
$.push();
54
$$payload.out += `<h1>hello world</h1>`;
6-
$.pop();
75
}

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

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

33
export default function State_proxy_literal($$payload, $$props) {
4-
$.push();
5-
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)