Bug SSR "Cannot access 'x' before initialization" with reactive let + autosubscribe
See original GitHub issueDescribe 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.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:7
- Comments:15 (11 by maintainers)
Top Results From Across the Web
bug ssr "Cannot access 'x' before initialization ... - Svelte
Interactive Svelte playground.
Read more >cannot access 'store' before initialization - You.com | The AI ...
Annoyance. The error mentioned in the title easily worked around by declaring the let variable outside of the reactive block: let x =...
Read more >How to debug "Cannot access X before initialization" errors?
For anyone getting here, I found this solution and it worked for me. Basically this error has two main reasons.
Read more >Adam Rackis on Twitter: "Not to sound like a broken record, but I'm ...
Bug SSR "Cannot access 'x' before initialization" with reactive let + autosubscribe · Issue #3582 ·... Describe the bug With SSR, this code:...
Read more >Cannot access x before initialization" - Ep 10 - YouTube
JS Casts 10 - How to fix "Uncaught ReferenceError: Cannot access x before initialization " in JavaScript.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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!