-
|
In this page component: <script lang="ts">
import { authenticatedUser } from '$lib/api/auth.svelte';
import { client } from '$lib/api/rumbleClient/client';
import { get } from 'svelte/store';
// value is present
const user = await authenticatedUser();
// this returns a promise which then resolves to an observable
const conferenceData = await client.liveQuery.conferenceUsers({
id: true
}, user.id);
// value is present on both, server and client side, this logs the array correctly in both consoles
console.log(get(conferenceData));
// if I add this, the values are correctly rendered during SSR AND in the browser
// console.log($conferenceData);
</script>
<!-- If I add this, the nothing gets rendered during SSR and the browser then correctly shows the data -->
<!-- {JSON.stringify($conferenceData)} -->
{#if !$conferenceData || $conferenceData?.length === 0}
<!-- this is redered in both SSR and browser when not adding one of the above snippets -->
nothing found
{:else}
{#each $conferenceData as c (c.id)}
{c.id}
{/each}
{/if}I have observed some weird behavior. Based on where I subscribe to an observable returned by my async function, the data is present or not and I can't really get my head around why. It seems to me that the observable is not correctly subscribed somehow. Unfortunately I was not able to reproduce this issue in the REPL. Does anyone know what could cause this? Thank you for your time! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
|
I made a small reproduction setup here: https://github.com/m1212e/sveltekit-store-issue-repro. See |
Beta Was this translation helpful? Give feedback.
-
|
This sounds like it might either be a bug or not be supported. |
Beta Was this translation helpful? Give feedback.
-
|
@m1212e this is a known edge case with svelte's store auto-subscriptions and async code during SSR. when svelte compiles the timing:
adding this is exactly why the fixes:
<script>
import { get } from 'svelte/store';
const data = await dataGenerator();
const initial = get(data);
</script>
{initial.id}
// +page.ts
export async function load() {
return { data: await fetchData() };
}
option 2 avoids the async + store footgun entirely and is the recommended sveltekit pattern. |
Beta Was this translation helpful? Give feedback.
This sounds like it might either be a bug or not be supported.
Would recommend opening an issue.