Repository navigation
awaiting a remote function in a component script for no-JS SSR prerendering?
#17312
Replies: 2 comments
|
I reproduced this on SvelteKit 2.70.3 / Svelte 5.57.1 with 1. Why the conditional always resolves A const q = { then: (resolve) => resolve('DATA') };
const f = async (cond) => (cond ? await q : q);
await f(false); // 'DATA', not qIf you really need a conditional, wrap the branches so the non-awaited one isn't a thenable: const result = $derived(cond ? { data: await myData } : { query: myData });2. You probably don't need the conditional at all Await unconditionally, either with <p>{await getData(id)}</p>During SSR this renders the resolved value, so the HTML works without JS. SvelteKit also serializes the result into the page ( On your first attempt ( (Small aside: the module is |
|
You don't need the <script>
const { someProp } = $props()
const myData = $derived(myRemoteFunc(someProp))
const value = $derived(await myData)
</script>During SSR the render waits for the query, so the HTML already has the data, and the result is serialized into the page. When JS is on, hydration reads it from that cache instead of calling the server again. I tried this on kit 2.70.3: one server call per page load, zero remote requests from the browser after hydration, and the same HTML with JS off. You can still keep Both things you ran into are expected: The bare The ternary always resolves because any Docs: https://svelte.dev/docs/kit/remote-functions#query and https://svelte.dev/docs/svelte/await-expressions (Small thing: it's |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I want to optionally allow clients to continue using the app without JavaScript, and this necessitates preloading all otherwise dynamic data during SSR. I thought I could do this by simply
awaiting the remote function calls that I would otherwise use to gather the data, during component initialization. I tried the following:But, the
asynchere does not block and the object still reports that the call isloading.I learned later that remote functions like this are not generally resolvable outside reactive scope due to caching, so I tried this:
This does work! But if I try to make it conditional, so that it only awaits with no JS:
then
myPreloadedDatawill always resolve, even if the conditional does not pass, and further (both synchronous and reactive) references tomyPreloadedDatawill be the results of the remote function.I tried a few more permutations and ended up with mixed results.
I think some of this may be buggy? I am not sure if so necessarily or if I'm running into expected quirks of the system, and I'd like to know what I can do to accomplish my goals.
I am using the latest SvelteKit 2. Async features are enabled of course.
All reactions