Stimulus Discourse

Controller initialized twice


#1

For some reason, my controller is being initialized twice.

At the moment, there is only one item on the page that uses this controller, but if I log when initialize is run, then I see two messages.

What could be causing this?

The reason for my concern is that some HTML is modified so if the controller initializes more than once, the markup is modified multiple times…

Any ideas how to prevent this from happening?

Also, I have seen a similar topic here but I am not using Turbolinks so that is not the issue.


#2

The only time I’ve seen this happen is in conjunction with Turbolinks (as you mentioned) when it was set up incorrectly.

Can you try to provide a minimal example showing the issue?


#3

Thanks Scott, I will see if I can put a simple demo together for you (it shouldn’t be too much trouble).


#4

See https://discourse.stimulusjs.org/t/controller-initialized-twice-when-visiting-from-a-turbolinks-page/17/3: