Skip to content

State referenced in onblur handler is undefined after navigation with async load #14575

@MathiasWP

Description

@MathiasWP

Describe the bug

Relevant issues:

Relevant fixes:

It seems like this bug occurs when:

  1. Some state is returned in a load method
  2. This load method has an await (e.g. fetching data remotely)
  3. The blur method is triggered from an input when navigating with the back/forward keys in the browser

I'm not really sure what the correct "mental model" is of this behaviour - but my two cents is that it feels wrong. It's really difficult to spot, as there's no typing that tells me that this can happen. The generated type-definition of let { data } = $props() is:

let data: {
    state: {
        message: string;
    };
}

It's really difficult to spot that this can happen since the type-definition says that state should always be defined in this context.

Reproduction

Github repro: https://github.com/MathiasWP/sveltekit-input-onblur-async-state-bug

Video:

Screen.Recording.2025-10-01.at.17.48.06.mov

Logs

+page.svelte?t=1759308998689:23 Uncaught TypeError: Cannot set properties of undefined (setting 'message')
    at HTMLInputElement.<anonymous> (+page.svelte?t=1759308998689:23:93)
    at events.js:61:21
    at without_reactive_context (shared.js:44:10)
    at HTMLInputElement.target_handler (events.js:60:11)
    at remove_effect_dom (effects.js:524:8)
    at destroy_effect (effects.js:471:3)
    at destroy_effect_children (effects.js:435:4)
    at destroy_effect (effects.js:475:2)
    at destroy_effect_children (effects.js:435:4)
    at destroy_effect (effects.js:475:2)

System Info

System:
    OS: macOS 15.6.1
    CPU: (8) arm64 Apple M1 Pro
    Memory: 274.14 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.14.0 - ~/.nvm/versions/node/v22.14.0/bin/node
    npm: 10.9.2 - ~/.nvm/versions/node/v22.14.0/bin/npm
    pnpm: 10.6.5 - /opt/homebrew/bin/pnpm
    bun: 1.0.0 - ~/.bun/bin/bun
  Browsers:
    Brave Browser: 139.1.81.137
    Chrome: 140.0.7339.208
    Edge: 140.0.3485.94
    Safari: 18.6
  npmPackages:
    svelte: ^5.0.0 => 5.39.7

Severity

annoyance

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions