Wrong controller retrieved in Production build

#8 · closed · 2 comments

View on GitHub ↗

AnthonySoler

Hello, After a production build `yarn build`, controllerim returns the wrong controller. The following scenario is based on the code available [here](https://github.com/AnthonySoler/controllerim_issue). Do not hesitate if you have any question. It is the first time I use controllerim so I may have missed something. Have a good day, ## Scenario ### Dev environment (OK) Run `yarn start` and open [http://localhost:3000](http://localhost:3000). Open the DevTools console to see the following traces: ``` OK HeaderComponent.js:19 OK MenuComponent.js:19 OK MenuComponent.js:28 ``` ### Prod environment (KO) Run `yarn build` then `serve -s build` Open [http://localhost:5000](http://localhost:5000). Open the DevTools console to see the following traces: ``` OK HeaderComponent.js:19 OK MenuComponent.js:19 KO MenuComponent.js:31 ``` ## Controllers tree ``` ApplicationController | |- HeaderController | |- MenuController ``` ## First analysis Controllerim uses `constructor.name` as key for controllers. In this example, after the minification process, the controllerName value is equal to "t" for every controller. So in the MenuComponent, the `this.controller.getParentController(HeaderController.name)` is equivalent to `this.controller.getParentController('t')`. In the `getControllerFromContext` function of controllerim, this name is used to find the related controller and return the first one which match this name. In our case, it will be ApplicationController whereas we want to retrieve the HeaderController and so the following verification failed. ```js if(headerController.iAmHeaderController) { console.log('OK'); } else { console.error('KO'); } ```

Comments

Niryo

hi, it was fixed in the @beta tag (V1.64.6) You should add a static prop to your controllers, with the name of the controller: `static controllerName = 'ParentController'; `, and If you forgot to do so, it will give you a warning in the console. I suggest creating a file with all the controller names as constants, and then use those constants both for registering the names in the static prop and for `getParentController()`. You can safely use the beta if you are not using `setStateTree` function or `addOnStateTreeChangeListener`. Thanks for the great analysis! I am now working on updating controllerim to new Context api, and supporting initialization of the controllers in the constructor instead of `componentWillMount`, so In few weeks from now (I am working on this only during the weekends) I will merge the beta tag with the latest tag.

AnthonySoler

Hello, Ok no problem. I was just working on a personal project during my free time learning ReactJS. I found a bypass but I wanted to be sure that you were aware of this issue. Thank you for your time and have a great day.