Repository navigation
awaiting a remote function in a component script for no-JS SSR prerendering?
#17312
|
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 <script>
import { browser } from "$app/env"
import { noJS } from "somePackage"
const { someProp, ...restProps } = $props()
const myData = $derived(myRemoteFunc(someProp))
if (!browser && noJS)
await myData
</script>But, the I learned later that remote functions like this are not generally resolvable outside reactive scope due to caching, so I tried this: const myPreloadedData = $derived(await myData)This does work! But if I try to make it conditional, so that it only awaits with no JS: const myPreloadedData = $derived((!browser && noJS) ? (await myData) : myData)then 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. |
Replies: 3 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 |
|
Disgusting AI answers that provided little helpful insight aside, I figured I should follow up for anyone else that encounters this issue. The solution that worked for me was to shift the This works because boundaries never do SSR when they have a This also properly caches and streams the result of the remote function call to the client when |
Disgusting AI answers that provided little helpful insight aside, I figured I should follow up for anyone else that encounters this issue.
The solution that worked for me was to shift the
awaitinto the component body, wrapped around a<svelte:boundary>. This then allows me to conditionally pass apendingfunction on the boundary, depending on if the page should be preloaded or not: