LAZY LOADING in IONIC2
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
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!
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.
at least lazy loading images like ion-img does in virtual lists would be good.
+1
+1
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";
@comfortme you can use this library to lazy load images:
https://github.com/NathanWalker/ng2-image-lazy-load
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.
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 so basically you have to include Ionic again in every module you lazyload? Does this slow things down?
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 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.
I am interesting too, please give an example or tutorial :)
@todrules, +1.
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.
+1
3 months later, do we have any update on this?
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 :(
@todrules and @compitDanielos About your solutions.... As soon as I've activated the router
BUT
The ion-content missed some fixed-margins .... How did you both set the thing up??
Thanks!!!!!
@Dutchboy my solution was, to retrieve relevant styling from ionic package, and import it in my project.