Repository navigation
fix: attach event handlers that are assigned after a top-level await in production - #18933
Merged
Merged
Conversation
…` in production In non-dev builds, an event attribute whose value is a local identifier passed that identifier to `$.event`/`$.delegated` directly. A declaration after a top-level `await` is assigned inside `$.run`, so the listener was attached with `undefined`. Dev builds were unaffected because `$.apply` reads the handler lazily. Bindings with a blocker now go through the same lazy wrapper as imports. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
Install the latest version of pnpm add https://pkg.svelte.dev/svelte/c/51df98629508b3024f5f08f6dc0a0026bcb71ea1Open in Note This PR is from a fork. A maintainer must approve approve each commit before it can be built and installed. |
🦋 Changeset detectedLatest commit: 51df986 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Merged
dummdidumm
pushed a commit
that referenced
this pull request
Oct 6, 2026
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated. # Releases ## svelte@5.57.2 ### Patch Changes - fix: don't create async deriveds for a component destroyed while waiting for its top-level `await` ([#18931](#18931)) - fix: prevent untracked derived reads from retaining disconnected dependencies ([#18829](#18829)) - fix: error if `{:else}` is followed by another `{:else}` or `{:else if ...}` ([#18900](#18900)) - fix: avoid tracking SvelteURLSearchParams during URL updates ([#18791](#18791)) - fix: restore hydration state when custom element attribute updates throw ([#18467](#18467)) - fix: attach event handlers that are assigned after a top-level `await` in production ([#18933](#18933)) - fix: flush anything pending before invoking flushSync callback function ([#18878](#18878)) - fix: preserve location information for `await` wrappers ([#18859](#18859)) - fix: keep a reactive assignment with no dependencies when `migrate` cannot turn it into a declaration ([#18817](#18817)) - fix: don't hang `migrate` on a declaration that shares a line with its script tag ([#18816](#18816)) - fix: prevent duplicate subscriptions when reconnecting deriveds ([#18899](#18899)) - fix: recover when an unexpected node replaces a nested boundary comment during hydration ([#18925](#18925)) - fix: print `{#await}` blocks that only have a pending branch ([#18908](#18908)) - fix: don't warn about a redundant `link` role on `<area>` elements without an `href` ([#18872](#18872)) - fix: don't throw `state_unsafe_mutation` when the focused element is removed while `bind:activeElement` is used ([#18921](#18921)) - fix: don't overwrite an unchanged spread `value`, preserving incomplete number input ([#18864](#18864)) - fix: read batch-local array on each-block commit ([#18879](#18879)) - fix: error at compile time when a declaration in a snippet redeclares one of its parameters ([#18842](#18842)) - fix: error when using `let:` directives on a component with a `children` snippet ([#18873](#18873)) - fix: ignore stale boundary reset callbacks ([#18886](#18886)) - fix: handle deferred transitions aborted before initialization ([#18776](#18776)) - fix: prevent hydration mismatch recovery from being intercepted by error boundaries ([#18841](#18841)) - fix: preserve dynamic element connections during hydration ([#18855](#18855)) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
jarrednorrisdev
added a commit
to jarrednorrisdev/effect-atom-svelte
that referenced
this pull request
Oct 6, 2026
Svelte 5.57.2 fixes event handlers assigned after a top-level await, which were undefined in production builds (sveltejs/svelte#18933, JND-56), and a component destroyed mid-await with an await in its markup throwing on `async_deriveds` (sveltejs/svelte#18931, JND-39). - Raise the svelte peer minimum to ^5.57.2, and the dev dependencies with it. - Remove "Handlers after an await" from Troubleshooting, and the mentions on the Suspense and React migration pages and in the hydration example. - Say Svelte 5.57.2 or later on the home and SvelteKit pages and in both READMEs. - Await in the script-await fixture's markup too, so the unmount-mid-load tests cover it. On 5.57.1 this fails with the async_deriveds error. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
dadezzz
pushed a commit
to dadezzz/events-cash-register
that referenced
this pull request
Oct 10, 2026
This PR contains the following updates: | Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) | |---|---|---|---| | [svelte](https://svelte.dev) ([source](https://github.com/sveltejs/svelte/tree/HEAD/packages/svelte)) | [`5.57.1` → `5.57.2`](https://renovatebot.com/diffs/npm/svelte/5.57.1/5.57.2) |  |  | --- ### Release Notes <details> <summary>sveltejs/svelte (svelte)</summary> ### [`v5.57.2`](https://github.com/sveltejs/svelte/blob/HEAD/packages/svelte/CHANGELOG.md#5572) [Compare Source](https://github.com/sveltejs/svelte/compare/svelte@5.57.1...svelte@5.57.2) ##### Patch Changes - fix: don't create async deriveds for a component destroyed while waiting for its top-level `await` ([#​18931](sveltejs/svelte#18931)) - fix: prevent untracked derived reads from retaining disconnected dependencies ([#​18829](sveltejs/svelte#18829)) - fix: error if `{:else}` is followed by another `{:else}` or `{:else if ...}` ([#​18900](sveltejs/svelte#18900)) - fix: avoid tracking SvelteURLSearchParams during URL updates ([#​18791](sveltejs/svelte#18791)) - fix: restore hydration state when custom element attribute updates throw ([#​18467](sveltejs/svelte#18467)) - fix: attach event handlers that are assigned after a top-level `await` in production ([#​18933](sveltejs/svelte#18933)) - fix: flush anything pending before invoking flushSync callback function ([#​18878](sveltejs/svelte#18878)) - fix: preserve location information for `await` wrappers ([#​18859](sveltejs/svelte#18859)) - fix: keep a reactive assignment with no dependencies when `migrate` cannot turn it into a declaration ([#​18817](sveltejs/svelte#18817)) - fix: don't hang `migrate` on a declaration that shares a line with its script tag ([#​18816](sveltejs/svelte#18816)) - fix: prevent duplicate subscriptions when reconnecting deriveds ([#​18899](sveltejs/svelte#18899)) - fix: recover when an unexpected node replaces a nested boundary comment during hydration ([#​18925](sveltejs/svelte#18925)) - fix: print `{#await}` blocks that only have a pending branch ([#​18908](sveltejs/svelte#18908)) - fix: don't warn about a redundant `link` role on `<area>` elements without an `href` ([#​18872](sveltejs/svelte#18872)) - fix: don't throw `state_unsafe_mutation` when the focused element is removed while `bind:activeElement` is used ([#​18921](sveltejs/svelte#18921)) - fix: don't overwrite an unchanged spread `value`, preserving incomplete number input ([#​18864](sveltejs/svelte#18864)) - fix: read batch-local array on each-block commit ([#​18879](sveltejs/svelte#18879)) - fix: error at compile time when a declaration in a snippet redeclares one of its parameters ([#​18842](sveltejs/svelte#18842)) - fix: error when using `let:` directives on a component with a `children` snippet ([#​18873](sveltejs/svelte#18873)) - fix: ignore stale boundary reset callbacks ([#​18886](sveltejs/svelte#18886)) - fix: handle deferred transitions aborted before initialization ([#​18776](sveltejs/svelte#18776)) - fix: prevent hydration mismatch recovery from being intercepted by error boundaries ([#​18841](sveltejs/svelte#18841)) - fix: preserve dynamic element connections during hydration ([#​18855](sveltejs/svelte#18855)) </details> --- ### Configuration 📅 **Schedule**: (UTC) - Branch creation - At any time (no schedule defined) - Automerge - At any time (no schedule defined) 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate CLI](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0NC4xNDguNCIsInVwZGF0ZWRJblZlciI6IjQ0LjE0OC40IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #18932.
The problem
With
experimental.async, an event handler whose value comes from a call after a top-levelawaitis never attached in a production build:The declaration after the
awaitis assigned inside$.run, but the event attribute passes the variable straight to$.delegated, while it's stillundefined:In a plain Vite app with
@sveltejs/vite-plugin-svelte, the button logsclickinvite dev, but aftervite build+vite previewit does nothing and nothing throws. Non-delegated events ($.event) and<svelte:window>handlers behave the same way. An attribute or component prop that reads the same kind of value works, since it already waits on$$promises[1].The cause
build_event_handlerreturns a local identifier as the handler as-is outside dev mode, on the assumption that the variable is already assigned when the listener is attached. A binding with ablockerisn't assigned at that point. Dev builds didn't show the bug because there the handler is always wrapped in$.apply(() => onclick, ...), which reads it when the event fires.The fix
Skip that shortcut when the binding has a
blocker. The handler then goes through the existing wrapper that imports already use,function (...$$args) { onclick?.apply(this, $$args); }, which reads the variable when the event fires. Handlers without a blocker compile exactly as before, so components without top-levelawaitdon't change.I considered wrapping the
$.event/$.delegatedcall in$.run_after_blockers, like #17198 did for attachments, actions, transitions, animations and bindings. Those need the value before they can run. An event listener doesn't: it can be attached at once and read the handler on dispatch, which is what dev mode already does. That keeps the change to one condition and leaves the attach order unchanged.Tests
runtime-runes/samples/async-event-handler-blockerscovers a delegated event (onclick), a non-delegated one (onmouseenter) and a<svelte:window>handler (onresize), each assigned from a call after a top-levelawait. Samples compile withoutdevby default, so the sample exercises the production path. It fails without the change ([]instead of['click', 'mouseenter', 'resize']in both thedomandhydratevariants) and passes with it.Test plan
pnpm vitest run packages/svelte/tests/runtime-runes/test.ts -t async-event-handler-blockers: 2 passed (2 failed onmain)pnpm test: 7,808 passed, 55 skipped, all 34 test files passingpnpm lint(ESLint and Prettier) andpnpm checkpassvite devandvite build+vite preview. Before the change, none fired in preview.Before submitting the PR, please make sure you do the following
feat:,fix:,chore:, ordocs:.packages/svelte/src, add a changeset (npx changeset).Tests and linting
pnpm testand lint the project withpnpm lint🤖 Generated with Claude Code