symfony / stimulus-bridge

Stimulus integration bridge for Symfony projects
https://symfony.com/ux
75 stars 15 forks source link

Lazy controllers connect twice #67

Closed jmsche closed 2 years ago

jmsche commented 2 years ago

Hi there,

I noticed a strange thing in my browser console yesterday:

image

After a little debugging, I found out that the culprit seems to be... configuring the controller as lazy using this code just above the controller class:

/* stimulusFetch: 'lazy' */

Tell me if there's more information I can provide to debug this.

jmsche commented 2 years ago

It seems the issue also exists when configuring Symfony UX controllers as lazy as well.

weaverryan commented 2 years ago

This is not something I realized would happen, but it's expected. See #67.

To get lazy controllers to work, we register a "fake" controller under your controller's name. Inside initialize(), that "fake" controller loads your REAL controller and registers it with Stimulus. So, the first initialize/connect that you see is for the "fake" controller. Then, immediately, your real controller replaces the "fake" one. The disconnect() that you see is the fake controller disconnecting.

So, this adds some unfortunate noise, but it's nothing to worry about. If you have code inside your controller's initialize() or connect() methods, it is only executed ONCE as expected.

Cheers!

jmsche commented 2 years ago

Thanks for the information :)