Closed trsteel88 closed 2 years ago
Hey!
Apologies for the slow reply. I'm not sure if this is possible. When you fetch "eager" (or don't have stimulusFetch
at all), it means that your Stimulus controller is actually packaged into the final, built JavaScript file - e.g. app.js
. So, once you build your assets, the code is either IN that file already, or not. There's no way at runtime to make this change.
The only way (I think) for this to work would be to "package the assets lazily". But then, as soon as the page loads, "trigger" the AJAX load to happen immediately. But there would still be a slight delay before they load. Probably the best option, but it increases complexity, is to have 2 different "entries" in Webpack - e.g. app.js
and admin.js
. Then, you would load the controllers differently to be lazy or not lazy. You would not be able to use the /* stimulusFetch: 'lazy' /
trick... as that would make it lazy for both the app
entry and admin
entry. You would need to load the controller manually (instead of auto-registering the entire controllers/
directory) using this crazy syntax to trigger the laziness - https://symfonycasts.com/screencast/stimulus/autocomplete-controller#codeblock-3b747a37ca (you could import it normally without all the craziness at the end for the non-lazy use).
I'm going to close the issue. I'm not sure there is enough demand for adding some systematic feature for this, and it would be complex.
Cheers!
Thanks for the response @weaverryan
I'm happy to have 2 entries (app.js and admin.js). I thought the build read the comments like annotations and then decided whether to package the script into app.js or to make it lazy.
What I am proposing is to have 2 entries but define groups on the annotation so it the controller could be lazy in 1 entry and eager in the other.
At the moment I am creating a duplicate controller in the admin folder and then extending the frontend controller but defining the lazy comment again. This is really cumbersome so having the ability to set an entry group on the comment would be much easier.
If you've got a vision for this, I'd love to see a PR - even if it is just a "proof of concept" type of thing for discussion :).
I'm wondering if it is possible to use the lazy loader with context.
I have a collection of controllers within a project which are shared between a Client area and and Admin area.
Within the Client area, I have controllers that I want them to load eagerly because of their frequency of use. However, for the admin area, I want them to load lazily aa they are only used on 1 or 2 pages.
It would be great if the annotation could have context like:
/ stimulusFetch: 'eager' / / stimulusFetch: (mode: 'lazy', groups ['admin', 'foo']) /
In the above example, the default would be to load as eagar, but for the admin area, load it lazy.