svelte: Bug SSR "Cannot access 'x' before initialization" with reactive let + autosubscribe
Describe the bug
With SSR, this code:
<script>
$: x = {}
</script>
{$x}
compiles to this:
// ...
$x = get_store_value(x);
let x = {}
// ...
x
is used before it is declared, and so it crashes. (It does not work with an actual store either.)
To Reproduce
https://svelte.dev/repl/e05965bb51ef4ab997af96e53dfc2a8c?version=3.12.1
(see ssr JS output)
Expected behavior
Same behavior as non SSR.
Information about your Svelte project:
- Svelte version 3.12.1
- REPL
Severity
Annoyance.
The error mentioned in the title easily worked around by declaring the let
variable outside of the reactive block:
let x = {}
$: x = {}
But what’s more annoying is that it forces to duplicate the code to compute the value, once for the manual declaration, and once for the reactive block. It can be cumbersome in some cases.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 7
- Comments: 15 (11 by maintainers)
This is finally fixed in 3.31.2 as part of a more sweeping change to how store autosubscriptions are handled in SSR code. See the generated code for https://svelte.dev/repl/e05965bb51ef4ab997af96e53dfc2a8c?version=3.31.2
Ayyyyy - happy to hear!