Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix components instantiated twice because of complex mutations #2376

Merged
merged 3 commits into from Nov 19, 2021

Conversation

SimoTod
Copy link
Collaborator

@SimoTod SimoTod commented Nov 15, 2021

This is mostly an issue with third-party libraries such as phoenix livewire which trigger complex mutations in certain scenarios, causing x-data scope to be multiplied, duplicate event listeners, x-init to run multiple times, etc.

Basically, they trigger multiple mutations containing the same components so alpine runs initTree multiple times by mistake.

This Pr leverage the existing _x_ignore functionalities to skip any redundant processing.

Failed test: https://github.com/SimoTod/alpine/runs/4211546805?check_suite_focus=true#step:6:900
After fix: https://github.com/SimoTod/alpine/runs/4211553560?check_suite_focus=true#step:6:900

@calebporzio
Copy link
Collaborator

Thanks for another deep fix @SimoTod! ❤️

@calebporzio calebporzio merged commit 282aacf into alpinejs:main Nov 19, 2021
@SimoTod SimoTod deleted the bug/complex-mutations branch November 20, 2021 00:28
@SimoTod SimoTod restored the bug/complex-mutations branch November 20, 2021 00:28
@f-liva
Copy link

f-liva commented Dec 14, 2021

I would like to point out that this change has indeed solved the double initialisation of components but now components dynamically injected into the dom (e.g. post ajax call) are not initialised, as well as any markup that simply uses alpine methods e.g. x-click, etc.

@SimoTod
Copy link
Collaborator Author

SimoTod commented Dec 14, 2021

hi @f-liva
do you have an example to show what you think the regression is?
It seems to work as expected from a quick check using the latest version: https://codepen.io/SimoTod/pen/qBPRMyQ
Also, the init test instantiates the component once as expected.

@f-liva
Copy link

f-liva commented Dec 15, 2021

The problem was caused by a <script></script> fragment in the content of the ajax response being injected into the page. In that fragment, the dom was manipulated again, and apparently Alpine did not notice this with the new change.

I solved it by moving that <script> fragment to the Alpine component, and everything works now.

Happy to provide further clarification if needed.

@SimoTod
Copy link
Collaborator Author

SimoTod commented Dec 15, 2021

@f-liva Yeah, if you can post a quick example, so I can check the behaviour before the PR and see if we can do anything, it would be great.
I tried myself but, by default, script tags don't get processed when injected in a page for security reasons so I need your specific setup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants