-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Store has undefined
value on SSR
#5949
Comments
I don't believe this is a bug, and it's not unique to Sapper either. This is simply how reactivity works in Svelte.
I believe what you are looking for in your case isn't to use import { onMount } from "svelte";
let favorites;
onMount(() => {
favorites = getFavoriteProducts();
}); |
Additionally - may I ask why you are creating the store in a reactive statement instead of immediately? I think just this should be enough, unless you have some special requirement. let favorites = getFavoriteProducts(); |
@c-bandy Thanks for commenting. The (pseudo) code I left under "Severity" only serves to illustrate why there is a need to have the variable declaration separate from the assignment, it's not the actual issue. The actual issue is the first code block. That code does not contain any And, yes, |
I believe this is fixed in v3.31.2 by #5419 |
The following code will print
{y: "value", x: "value"}
in the browser console if you run it in the REPL or on Sapper client side, but running it on the server side in Sapper will result in{ y: undefined, x: 'value' }
.The difference between
x
andy
is only the assignment to the variable happening separately from the definition.This feels like bug because:
undefined
if it's assigned an initial value...but it's of course possible this is intended behavior :D In that case I'd be curious as to why this happens.
I reported it in Sapper because I can only produce the issue in Sapper, but it might be a Svelte core issue.
Information about your Sapper Installation:
Severity
Moderate. I have a template looking something like
which won't work due to the above. The following does work but results in duplicate creation of the store
(
$: favorites = getFavoriteProducts();
would work but I can't do that either because I get a different error if I reference it in a separate reactive block)The text was updated successfully, but these errors were encountered: