Skip to content

Commit bfcf4ea

Browse files
committed
fix: move store setup/cleanup outside of async component body
1 parent 05f6436 commit bfcf4ea

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

.changeset/new-fireants-bake.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: move store setup/cleanup outside of async component body

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ export function client_component(analysis, options) {
209209

210210
/** @type {ESTree.Statement[]} */
211211
const store_setup = [];
212-
212+
/** @type {ESTree.Statement} */
213+
let store_init = b.empty;
213214
/** @type {ESTree.VariableDeclaration[]} */
214215
const legacy_reactive_declarations = [];
215216

@@ -227,8 +228,9 @@ export function client_component(analysis, options) {
227228
if (binding.kind === 'store_sub') {
228229
if (store_setup.length === 0) {
229230
needs_store_cleanup = true;
230-
store_setup.push(
231-
b.const(b.array_pattern([b.id('$$stores'), b.id('$$cleanup')]), b.call('$.setup_stores'))
231+
store_init = b.const(
232+
b.array_pattern([b.id('$$stores'), b.id('$$cleanup')]),
233+
b.call('$.setup_stores')
232234
);
233235
}
234236

@@ -379,9 +381,16 @@ export function client_component(analysis, options) {
379381
analysis.slot_names.size > 0;
380382

381383
if (analysis.instance.has_await) {
384+
const params = [b.id('$$anchor')];
385+
if (should_inject_props) {
386+
params.push(b.id('$$props'));
387+
}
388+
if (store_setup.length > 0) {
389+
params.push(b.id('$$stores'));
390+
}
382391
const body = b.function_declaration(
383392
b.id('$$body'),
384-
should_inject_props ? [b.id('$$anchor'), b.id('$$props')] : [b.id('$$anchor')],
393+
params,
385394
b.block([
386395
b.var('$$unsuspend', b.call('$.suspend')),
387396
...component_block.body,
@@ -397,10 +406,12 @@ export function client_component(analysis, options) {
397406
component_block = b.block([
398407
b.var('fragment', b.call('$.comment')),
399408
b.var('node', b.call('$.first_child', b.id('fragment'))),
400-
b.stmt(b.call(body.id, b.id('node'), should_inject_props && b.id('$$props'))),
409+
store_init,
410+
b.stmt(b.call(body.id, b.id('node'), ...params.slice(1))),
401411
b.stmt(b.call('$.append', b.id('$$anchor'), b.id('fragment')))
402412
]);
403413
} else {
414+
component_block.body.unshift(store_init);
404415
component_block.body.push(.../** @type {ESTree.Statement[]} */ (template.body));
405416
}
406417

0 commit comments

Comments
 (0)