r/angularjs • u/permabed • Oct 22 '14
[Help] AngularJS, lazy loading, angularAMD, ocLazyload
Hello guys,
i need your point of view. i'm building a large, very large app using angularJS. We need lazy loading and i'm looking for multiple possible solution. What do we need to lazy load ? Since we have a large app, we sliced them in little angular module and try to download JS and load the needed angular modules only when needed.
Why ? Sometime user will log and only use a module of our app. So, we don't need to load all the JS files. Even if the JS files are in the browsers cache, we don't want to load each angular modules for performance reasons. Another use case is for the configuration panel. Why loading the profile configuration module when it is used only sometimes.
angularAMD (https://github.com/marcoslin/angularAMD) seems a good solution. The dev is really active but it seems a quick technical response for the question : 'how can i use requirejs with angular' and not 'how to make lazy loading with angular'. The result is to have to use 2 module descriptor for each module : one for requirejs, and one for angular. The worst case is to have to describe the same component 2 times for each dependency manager : one for require for the dependency tree (which files it has to load) and one for AngularJS dependency system. For a maintenance point of view it's really not a good and viable strategy when we have hunder of files. Furthermore, the angularAMD solution seems to be really intrusive in the angular code and we can have an issue for maintenance/upgrading.
ocLazyLoad (https://github.com/ocombe/ocLazyLoad). The dev seems to try to respond to my main question : 'how to make lazy loading with angular?'. It can use requirejs as lazy loader or script.js. It try to use the dependency descriptor of angularjs to load required files so the solution is less intrusive than angularAMD. But, it's seems to be not very used by lot of people. When i look on the net for lazy load with angular a lot of people is going for the requirejs approch :'define("...", ...'. And then come my next issue. If i choose ocLazyLoad and an angular 1.3 fix come out, or angular 1.4 or even 2.0, it can be a pain in the ass to move to the newer angular solution and be stuck on the current branch.
Now, for the angular team. It's seems that they don't even bother about this question. Angular evangelist tell us to use Angular even for a hello world app but whenever we have this question asked, it seems they avoid this point by giving a really elusive response (https://docs.angularjs.org/guide/module => Asynchronous Loading). I took a look to the ng conf video 2014 here http://www.youtube.com/watch?v=4yulGISBF8w . It's interesting but the guy explain how to handle dependecy tree with RequireJS, not lazy loading as AngularAMD try to handle : lazy load an entire angular module when needed.
Angular 2.0 ... seems to have switched to ES6 using traceur. The good point with this path is that we can use import, export of ES6 and traceur can transpile code to ES5 by having module translated to any other ES5 dependency solution. But, they also use annotation which is not in the ES6 spec and it seems to be a threat to the adoption of the 2.0 solution. But, let assume that ES6 module is the solution and pray that all the modern browser will support them and take a look to :
https://status.modern.ie/moduleses6?term=module => under consideration
https://bugzilla.mozilla.org/show_bug.cgi?id=568953 => not closed yet
chrome .. didn't found the status yet but since ng 2.0 is going full ES6, i think it will be available soon
I don't even talk about the refactoring point which is a completely another discussion. But if i have to move a resource from one folder to another, it seems that i have to go in impacted files and fix manually the dependency...
And finally, all cited resources are open source initative (except IE ;) ) and thanks to them. You are awesome to share those with the world. Thanks! I hope you will not take negativly my questions ;)
1
u/jacquesdancona Oct 29 '14
Using ocLazyLoad, our app is huge. My routes are set up to use certain modules, and those modules may have dependencies for themselves. My goal was to be completely modular, and every module can pretty much run itself (requesting other modules when needed).
Also, some modules depend on stuff other than angular (video players, jQuery plugins, etc) which the module can load for itself. Some modules aren't loaded at first, but can be loaded when a user needs is (for example, media library module).
Ultimately, a client just loads what is needed. The homepage, when no session, is just a couple of kb's because there's hardly any logic there.