[4.x] Fix lazy components receiving events before mount#10217
Merged
calebporzio merged 1 commit intomainfrom Apr 11, 2026
Merged
[4.x] Fix lazy components receiving events before mount#10217calebporzio merged 1 commit intomainfrom
calebporzio merged 1 commit intomainfrom
Conversation
3 tasks
Collaborator
|
makes sense! |
This was referenced Apr 13, 2026
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.
The Scenario
When a lazy-loaded component has an event listener (via
#[On]or$listeners) and hasn't been mounted yet (e.g., it's outside the viewport), dispatching that event causes the component to receive it and render withoutmount()ever being called. This leads to errors because properties initialised inmount()are uninitialised.A common real-world case is a page with a list of items, each containing a modal with lazy-loaded tab content. When one item dispatches a refresh event, lazy components in other items that haven't been opened yet receive the event and break.
The Problem
When a lazy component is initially rendered,
mount()is skipped and a placeholder is output instead. However,SupportEventsstill adds the component's event listeners to the dehydrated effects during this placeholder mount. On the JavaScript side,supportListeners.jsregisters globalwindowevent listeners and element-level listeners for "to"/"self" dispatches immediately, with no check for whether the component has actually been mounted.When an event is dispatched, the JS handler fires
component.$wire.call('__dispatch', ...), sending a server request that calls the listener method on a component whosemount()was never executed.The Solution
Added a guard in both the global and element-level listener handlers in
supportListeners.jsto skip event dispatch when the component is lazy and hasn't been loaded yet:This uses the same pattern already established in
component.jsfor skipping reactive prop updates on unmounted lazy children. Once the component is lazy-loaded viax-intersect,hasBeenLazyLoadedbecomestrueand events work normally.Fixes #10216