Skip to content

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

Closed
@rixo

Description

@rixo

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.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions