Skip to content

Commit 307ec22

Browse files
authored
fix: move store setup/cleanup outside of async component body (#16443)
1 parent ad0b58e commit 307ec22

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

@@ -385,9 +387,16 @@ export function client_component(analysis, options) {
385387
analysis.slot_names.size > 0;
386388

387389
if (analysis.instance.has_await) {
390+
const params = [b.id('$$anchor')];
391+
if (should_inject_props) {
392+
params.push(b.id('$$props'));
393+
}
394+
if (store_setup.length > 0) {
395+
params.push(b.id('$$stores'));
396+
}
388397
const body = b.function_declaration(
389398
b.id('$$body'),
390-
should_inject_props ? [b.id('$$anchor'), b.id('$$props')] : [b.id('$$anchor')],
399+
params,
391400
b.block([
392401
b.var('$$unsuspend', b.call('$.suspend')),
393402
...component_block.body,
@@ -403,10 +412,12 @@ export function client_component(analysis, options) {
403412
component_block = b.block([
404413
b.var('fragment', b.call('$.comment')),
405414
b.var('node', b.call('$.first_child', b.id('fragment'))),
406-
b.stmt(b.call(body.id, b.id('node'), should_inject_props && b.id('$$props'))),
415+
store_init,
416+
b.stmt(b.call(body.id, b.id('node'), ...params.slice(1))),
407417
b.stmt(b.call('$.append', b.id('$$anchor'), b.id('fragment')))
408418
]);
409419
} else {
420+
component_block.body.unshift(store_init);
410421
component_block.body.push(.../** @type {ESTree.Statement[]} */ (template.body));
411422
}
412423

0 commit comments

Comments
 (0)