Repository navigation
Fix morphBetween always using cloneNode even when node is detached - #4923
Open
ghabriel25 wants to merge 4 commits into
Open
ghabriel25 wants to merge 4 commits into
ghabriel25 wants to merge 4 commits into
Conversation
morphBetween always using cloneNode even when node is detached
ghabriel25
force-pushed
the
fix/morph-between
branch
from
September 30, 2026 16:11
0af2487 to
6d51d03
Compare
This reverts commit 63be3c4.
3 tasks done
ghabriel25
marked this pull request as ready for review
September 30, 2026 16:41
This branch has not been deployed
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.
Summary
Fixes duplicate media resource loads (e.g.
<video>/<source>) when morphing detached HTML trees throughmorph/morphBetween.Alpine always used
node.cloneNode(true)when inserting nodes from the “to” tree. For detached morph blueprints that duplicates media elements, this can trigger a second network fetch (notably with Livewire deferred islands — livewire/livewire#10763).Root cause
In
addNodeBefore, the empty-fromappend path, andswapElements, the implementation did:The live DOM always received a clone of the temporary to-tree. Media elements in that clone start resource selection again, so the same asset can load twice.
Passing a string vs a pre-built element does not matter: both end in the same clone path.
Solution
Introduce
transferNode:!node.isConnected(detached morph blueprint) → move the node (no second media element)node.isConnected(live node used as “to”) →cloneNode(true)(do not steal it from the document)Apply
transferNodeonly where the to-node is not needed again as a blueprint in the same step:swapElementstransferNodefromappendtransferNodeaddNodeBeforecloneNode(true)addNodeBeforestill clones because lookahead can insert then fall through topatch(currentFrom, currentTo). MovingcurrentTothere would makepatchrun against the same node.When moving is possible, capture the next “to” sibling before the move so the to-walk stays valid:
Fixes: livewire/livewire#10763