symfony / stimulus-bridge

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

Duplicated controller instances #27

Closed zmitic closed 3 years ago

zmitic commented 3 years ago

I am using latest bridge and even default hello_controller.js can be used to replicate bug.

For HTML like this:

    <div data-controller="hello" data-hello-name="1"></div>
    <div data-controller="hello" data-hello-name="2"></div>

and hello_controller.js like this:

import { Controller } from 'stimulus';

/* stimulusFetch: 'lazy' */
export default class extends Controller {
    static targets = [ "name" ];

    connect() {
        console.log(this.element.getAttribute('data-hello-name'));
    }
}

each block is instantiated twice:

image


If I add third:

    <div data-controller="hello" data-hello-name="1"></div>
    <div data-controller="hello" data-hello-name="2"></div>
    <div data-controller="hello" data-hello-name="3"></div>

each is loaded 3 times:

image


Problem is gone if I remove /* stimulusFetch: 'lazy' */ or there is only 1 controller.

I followed all the steps from upgrade docs to make sure I don't mess anything (I am noob when it comes to JS).

weaverryan commented 3 years ago

Hey @zmitic!

Thanks for the very detailed report! This looks legit - it looks like we're missing some tracking to not re-register the lazy controllers. I'm checking into it right now :).

Cheers!

weaverryan commented 3 years ago

Fixed in #28!

zmitic commented 3 years ago

Thank you, that was super fast. Will you tag it or do I use dev-master?

weaverryan commented 3 years ago

We will tag it tomorrow (Sunday) :)