Skip to content

Async Svelte crash in certain situations when destructuring awaited derived value in a @const #18024

@dimfeld

Description

@dimfeld

Describe the bug

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.

Reproduction

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.

Logs

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)

System Info

SvelteLab, also reproduced locally with latest Svelte and SvelteKit

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions