Skip to content

Commit 669a32a

Browse files
fix: destructuring state in ssr (#16102)
1 parent d5c6991 commit 669a32a

File tree

6 files changed

+28
-1
lines changed

6 files changed

+28
-1
lines changed

.changeset/silly-turkeys-confess.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: destructuring state in ssr

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,13 @@ function create_state_declarators(declarator, scope, value) {
197197
}
198198

199199
const tmp = b.id(scope.generate('tmp'));
200-
const { paths } = extract_paths(declarator.id, tmp);
200+
const { paths, inserts } = extract_paths(declarator.id, tmp);
201201
return [
202202
b.declarator(tmp, value), // TODO inject declarator for opts, so we can use it below
203+
...inserts.map(({ id, value }) => {
204+
id.name = scope.generate('$$array');
205+
return b.declarator(id, value);
206+
}),
203207
...paths.map((path) => {
204208
const value = path.expression;
205209
return b.declarator(path.node, value);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0, 1
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script lang="ts">
2+
let count = 0;
3+
function* test(){
4+
while (true) {
5+
yield count++;
6+
}
7+
}
8+
let [one, two] = $state(test())
9+
</script>
10+
11+
{one}, {two}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
10, Admin
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script lang="ts">
2+
let [level, custom] = $state([10, "Admin"])
3+
</script>
4+
5+
{level}, {custom}

0 commit comments

Comments
 (0)