-
Notifications
You must be signed in to change notification settings - Fork 426
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
Stimulus controllers imported twice, Initialize and clickEvents firing twice #777
Comments
Here's also my manifest.js file:
|
It looks like if (canRegisterController(name, application)) in stimulus-loading.js calls .then(module => registerController(name, module, application)) |
I found out that the problem gets solved when commenting out the es-module-shims import. Closing this issue here. If you think that you can propose a workaround to this esm shims issue, feel free to re-open it. |
Sorry, my mistake. I've removed the esm-shims but the controllers still get loaded and initialised twice. I also had the importmaps twice in the application layout, no idea why and how, but removing the second one also didn't change anything. And I deactivated rails_live_reload because I thouthg that might interfere, with no effect. |
Similar problem to this issue, but my Stimulus controllers even fire Initialize twice with just one controller instance. I'm using ruby "3.2.2", "rails", "~> 7.1.3", ">= 7.1.3.4" with Importmaps, es-module-shims@1.10.0, TailwindCSS and ViewComponents. My Stimulus controllers get pinned in the importmap:
then in talwind.config.js:
(Please also take note of the fact that I had to put the components into a subdir like frontend for the Stimulus controllers to get loaded at all, but that's another issue)
Then my app/javascript/application.js looks like this:
app/javascript/controllers/index.js:
app/javascript/controllers/application.js:
(default, I haven't made any changes here)
And, finally, my audio_player_component.html.erb, which has only one instance that' included directly in the application layout:
(take note of the data-turbo-permanent attribute)
and its Stimulus controller
(the latter two reside under app/frontend/components)
I expect that my component and its controller are a bit buggy, this is a WIP version. The point is that this component is only existent once in the DOM, in an audio player comopnent that's fixed to the bottom of the page and has to be persistent over page navigation. Just as all the other component controllers, it gets initialized twice:
For comparison, this is another non-ViewComponent-controller's log output, which also gets initialised twice:
Resulting behaviour is that all eventListeners get attached twice, resulting in button clicks not being performed once as expected, but twice, resulting in faulty behaviour of especially toggle-buttons. The way I've set it up now, the eventListeners defined in the Initialize() function get called only once, but those that aren't defined here, like those for Stimulus' data-actions, still fire twice.
Expected behaviour: fire once
Steps to reproduce:
Based on my research, this is most likely due to Turbo caching issues. Since Stimulus and Turbo are promoted as working together perfectly but obviously they don't, I'd suggest adding caching excemptions for Stimulus controllers, or at least some sort of switch to easily turn this behaviour, which seems to be intended, off for certain controllers or functions.
Please let me know if I'm wrong or if such a solution does already exist. I'm pretty new to Hotwire and I didn't find anything about this issue in the docs.
The text was updated successfully, but these errors were encountered: