-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Bug SSR "Cannot access 'x' before initialization" with reactive let + autosubscribe #3582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Ok... but you didn't declare When a store is auto-subscribed, an internal function is called to handle the subscription and a variable of the same name is created in the component context during initialization. The real question is why a |
@dkondrad It does not work with an actual store either. The problem is that |
@rixo, yeah I'm well aware of the temporal dead zone. Something I forgot to ask was why are you creating a store with a reactive statement anyway? I'm not a maintainer, but I believe this falls squarely in the "it hurts when I do this" (i.e. don't do this) category. |
@dkondrad Well, OK I'm sorry for sending you a tutorial. I took some time in the REPL to pinpoint a minimal repro so I've been a little vexed by your rebuttal "it's on you because it's not a store" to my "(it does not work with an actual store either)", and felt a little tease on your own mistake was fair game. I don't think "let's not fix the bug because the reporter's a jerk" is a constructive response either though. So, my use case is an <script>
export let source
// ...
// introspect source to pick the right adapter for the source type
$: wrap = resolveWrapper(source)
// wrap outputs a store that streams promises, according to the source emisions
$: wrappedPromise$ = wrap(source)
</script>
{#await $wrappedPromise$}
...
{/await} The store in question is created dynamically based on I would think twice if I had to write the boilerplates by hand, but in Svelte you combine two simple (simple for the user, that is) atoms and it becomes a one-liner. Svelte's awesome. ❤️ ❤️ ❤️ |
@rixo, fair enough... I accept your tease :) You should have just led with that example in the first place because that makes a whole lot more sense! I still not sure why a store is needed instead of a plain-ole-promise, but you know more about your problem domain. Maybe this will be better handled by Rich's code-red branch. |
just linking a possible terser bug for future people googling this problem sveltejs/template#81 - fixed my issue, could fix yours |
I've also run into this issue. Here's a reproduction case with a bit more context. It toggles between two different stores based on the value of a checkbox. This runs fine in the browser, but the SSR build tries to read |
Is this issue still valid? (was it ever?)
fails for me with
with or without SSR, which makes sense. Changing it to an actual store (or presumably an object with a subscribe method) solves it with or without SSR. |
Yes it's still current: https://svelte.dev/repl/e05965bb51ef4ab997af96e53dfc2a8c?version=3.29.7 It's not SSR code running in the REPL, that's why you don't see the bug. Check the compiled JS output, there's a runtime error in it. |
If you just change the empty object literal to properly implement the store interface, it works fine. https://svelte.dev/repl/4133893d920a4458a25e734a826b9509?version=3.29.7 |
Oh yikes - yeah that's definitely a bug :-\ Looks like a fix was started here #5419 but never finished. |
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! |
Describe the bug
With SSR, this code:
compiles to this:
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:
Severity
Annoyance.
The error mentioned in the title easily worked around by declaring the
let
variable outside of the reactive block: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.
The text was updated successfully, but these errors were encountered: