You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
x-data's init() runs twice when morph() patches an existing node to add x-data #4900
x-init is skipped while Alpine clones a tree internally (skipDuringClone() in x-init.js). x-data's own init() method isn't (x-data.js:60). Since Alpine.morph() calls Alpine.cloneNode() when patching an existing node, adding x-data to an element via morph runs its init() twice: once on morph's throwaway diff node, once for real when the attribute lands live. New nodes aren't affected, only patched ones.
I ran into this while working with Livewire's lazy component. A placeholder element with no x-data gets morphed into the real, concrete component markup, which does have x-data. I had an error in the console because x-data's init() was trying to access children x-refs that aren't available yet during the patch of cloneNode.
Code snippets to reproduce the issue
Open this in a browser and check the console: you'll see two logs. Replace x-data's init() with an x-init="console.log(1)" attribute instead, and you'll only get one.
Activity