The issue involves complex async behavior and template destructuring in Svelte.
The issue occurs when destructuring an awaited derived value in a `@const` statement within a Svelte template, causing a crash. The problem seems related to how Svelte handles async values and destructuring in templates. The scope is somewhat clear, but understanding Svelte's internal handling of async and derived values is required.
This seems to happen when you have:
First, an awaited query, and a derived expression from that query
import { getIt } from './query.remote.ts';
let d = $derived(await getIt());
// For some reason this additional derived is required to reproduce
let showFetchCta = $derived(d.hasData)
In the template, a @const statement that destructures the awaited value.
{#if d}
{@const {data, hasData} = d}
{#if hasData}
<p>The Data: {data}</p>
{:else if showFetchCta} <!-- reference the derived so it doesn't get thrown away -->
<p>Fetch now</p>
{:else}
<p>No data</p>
{/if}
{/if}
Causes the error
TypeError: Cannot read properties of undefined (reading 'f')
In Module.get where it reads signal.f (signal is apparently still undefined).
If you remove the @const destructuring and just reference the values as d.data and so on, then it works fine.
https://www.sveltelab.dev/38gut2mu4bw9p4d
Note you might have to run with the inspector open to really see the crash. Otherwise it's not totally obvious all the time.
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'f')
in <unknown>
in +page.svelte
in +layout.svelte
in root.svelte
in undefined
at Module.get (chunk-O3RU4Q5E.js?v=4ba18bce:4157:23)
at +page.svelte?t=1774689309840:99:13
at chunk-GMPPHB2E.js?v=4ba18bce:798:5
at update_reaction (chunk-O3RU4Q5E.js?v=4ba18bce:3983:18)
at update_effect (chunk-O3RU4Q5E.js?v=4ba18bce:4122:21)
at create_effect (chunk-O3RU4Q5E.js?v=4ba18bce:3466:7)
at block (chunk-O3RU4Q5E.js?v=4ba18bce:3632:17)
at Module.if_block (chunk-GMPPHB2E.js?v=4ba18bce:796:3)
at +page.svelte?t=1774689309840:98:16
at Module.add_svelte_meta (chunk-O3RU4Q5E.js?v=4ba18bce:600:12)
SvelteLab, also reproduced locally with latest Svelte and SvelteKit
annoyance
Claim this issue to let others know you're working on it. You'll earn 20 points when you complete it!