r/angularjs • • 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 :

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 ;)

8 Upvotes

5 comments sorted by

View all comments

1

u/magicaj Oct 22 '14

One low-fi solution is to just have multiple SPAs, that is, multiple HTML page entry points that segment the app and its dependencies how you want. In your case this may result in an HTML entry point per module, or perhaps a single entry point that parameterizes the module name to determine which script to load.

In the case where you need to "bridge" from one SPA to another you would just do a full page refresh, perhaps that is the 20% use case when using a targeted portion of your app. You could then be smart about how to serve up the portions to try and maximize local caching.