question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug SSR "Cannot access 'x' before initialization" with reactive let + autosubscribe

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:7
  • Comments:15 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
Conduitrycommented, Jan 4, 2021

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

1reaction
arackafcommented, Jan 5, 2021

Ayyyyy - happy to hear!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found