ionic-framework icon indicating copy to clipboard operation
ionic-framework copied to clipboard

LAZY LOADING in IONIC2

Open mohammedzamakhan opened this issue 10 years ago • 43 comments

Short description of the problem:

Angular Router (Angular 2) has the ability to lazy load components, since Ionic is using its own router, will we have the functionality to lazy load the components?

What behavior are you expecting?

Lazy Loading Components

mohammedzamakhan avatar Sep 16 '16 18:09 mohammedzamakhan

Hello, thanks for opening an issue with us! Lazy loading is something that we are researching at the moment but from our current tests/thoughts we are not totally convinced that lazy loading of components gives a major boost to performance. I will leave this issue open as a feature as it is something that we want to research. Thanks!

jgw96 avatar Sep 16 '16 18:09 jgw96

For a large scale web app, which has 100s of routes, if we do not lazy load, the initial load time would be a lot, because you have to load all the js at the beginning.

mohammedzamakhan avatar Sep 16 '16 19:09 mohammedzamakhan

at least lazy loading images like ion-img does in virtual lists would be good.

comfortme avatar Sep 17 '16 08:09 comfortme

+1

05bca054 avatar Sep 17 '16 14:09 05bca054

+1

rehiy avatar Sep 19 '16 12:09 rehiy

We have large app with many modules. Main problem was to deal with BrowserModule loaded twice. Once you use lazy loading, you should load this in main NgModel, than in other modules just add CommonModule.

BrowserModule was loaded twice beacose of Ionic imports this in IonicModule.

Workaround I found is in ionic-angular/module.js line ~101 Change

imports: [BrowserModule, HttpModule, FormsModule, ReactiveFormsModule], exports: [BrowserModule, HttpModule, FormsModule, ReactiveFormsModule, IONIC_DIRECTIVES] to imports: [CommonModule, HttpModule, FormsModule, ReactiveFormsModule], exports: [CommonModule, HttpModule, FormsModule, ReactiveFormsModule, IONIC_DIRECTIVES]

And add import import { CommonModule } from "@angular/common";

compitDanielos avatar Oct 19 '16 15:10 compitDanielos

@comfortme you can use this library to lazy load images:

https://github.com/NathanWalker/ng2-image-lazy-load

cleever avatar Oct 19 '16 15:10 cleever

I'd like to see this implemented as well. I'm building an app that while may not have a lot of modules, the modules that it does have could affect performance if all were loaded on app init.

For example, let's say the app has Facebook, Twitter, Google+, LinkedIn, and Slack modules, but the user only uses Facebook. Then why load the others? Maybe there's a config option I'm missing that can dynamically load modules during bootstrap based on env/system/user settings and configurations, but I couldn't find anything.

Any feature updates or workarounds would be greatly appreciated.

todrules avatar Oct 20 '16 00:10 todrules

To answer my own post, I did get lazy loading working for Ionic using Webpack 2. I also have HMR and AOT working, as well, so it is possible with a little trial and error. I'm also using Angular's router in my Ionic apps, so I'm not sure how this would be done with the Ionic NavController, but it's pretty easy to switch out, and even the syntax is almost identical.

The main issue that I ran across is that IonicModule imports BrowserModule from Angular, so when you try to import Ionic into the lazy-loaded module, it fails. You can only import BrowserModule once.

The solution I finally got to work was to replace BrowserModule with CommonModule in my local Ionic build. It's actually pretty easy. There's just 3 spots in module.ts/js that need to be replaced. Finally, make sure your AppModule imports BrowserModule, then Ionic can be imported multiple times in your app, permitting lazy loading.

todrules avatar Oct 26 '16 15:10 todrules

@todrules so basically you have to include Ionic again in every module you lazyload? Does this slow things down?

emmawi avatar Oct 26 '16 19:10 emmawi

It doesn't slow it down at all. Ionic is already in memory. You're just importing it into the new module just like you would do with CommonModule or FormsModule.

todrules avatar Oct 27 '16 02:10 todrules

@todrules Do you have an example of your lazy loading working any where? I'm working on a pretty big application that will be web and mobile based and would love to be able to lazy load multiple modules after the application has initialized.

alexthebuilder avatar Nov 04 '16 21:11 alexthebuilder

I am interesting too, please give an example or tutorial :)

evgeniynet avatar Nov 14 '16 09:11 evgeniynet

@todrules, +1.

hoisel avatar Nov 15 '16 20:11 hoisel

Just a follow up. I got an example working with lazy loading. Since Lazy loading does not work with AOT at the moment there is no real win with this. Right now I am more interested in getting Angular Universal working with Ionic. Once AOT works with lazy loading I think it may be worth another look. Mainly for a separate build process for PWA.

alexthebuilder avatar Nov 15 '16 21:11 alexthebuilder

+1

alwayrun avatar Dec 20 '16 05:12 alwayrun

3 months later, do we have any update on this?

ivaylopivanov avatar Dec 22 '16 21:12 ivaylopivanov

I am looking forward to use Ionic to build Progressive Web App, but this one feature is a big blocker for me, I want to see lazy loading in Ionic Applications :(

mohammedzamakhan avatar Jan 06 '17 18:01 mohammedzamakhan

@todrules and @compitDanielos About your solutions.... As soon as I've activated the router and commented-out all the nav-controller and <ion-nav [root]="rootPage"> .... the mini app works again.....

BUT

The ion-content missed some fixed-margins .... How did you both set the thing up??

Thanks!!!!!

Dutchboy avatar Jan 10 '17 22:01 Dutchboy

@Dutchboy my solution was, to retrieve relevant styling from ionic package, and import it in my project.

compitDanielos avatar Jan 11 '17 08:01 compitDanielos