fix: ensure deferred effects are properly revived after async operations (fixes #17304)fix: ensure deferred effects are properly revived after async operati… #17307
+29
−8
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 #17304
Problem Description
When using
asyncin SvelteKit with preloading data (viadata-sveltekit-preload-dataorpreloadData()),$effectcallbacks stop firing even though$inspectshows the values are updating correctly.Reproduction
The issue can be reproduced with the StackBlitz example: https://stackblitz.com/edit/sveltekit-reactivity-loss
Steps to reproduce:
data-sveltekit-preload-data="hover"on a link$effectto open a dialog$effectdoesn't fireRoot Cause
The issue occurs in the batch system's
revive()method inpackages/svelte/src/internal/client/reactivity/batch.js.When SvelteKit's preload functionality is used, it creates a "fork" to speculatively load data. When this fork is committed, it calls
batch.revive()to reschedule any deferred effects. However, therevive()method wasn't ensuring that the batch was properly activated before scheduling effects.The sequence of events:
#dirty_effectsand#maybe_dirty_effectsarraysCLEANto allow them to be rescheduled laterrevive()is called to reschedule these effectsschedule_effect()would add effects toqueued_root_effects, but they wouldn't be processed correctly because the batch context wasn't activeSolution
The fix ensures that:
Changes Made
Modified the
revive()method inbatch.jsto:Impact
This fix ensures that effects are properly revived after async operations complete, which is critical for:
fork()APIThe change is minimal and focused on ensuring the correct batch context