Optimize App Loading by Implement Lazy Loading in Ionic 3 Existing/ New App

An Ionic framework application makes development very easy and fast for multiple platforms, but sometimes due to performance issues like fluctuation in animations, jerk movements during user interactions calls for some tricky solutions to manage code with better optimizations.

One such issue is startup time of Ionic applications, with more views an application startup time increases many folds due to a number of components loading at once during app initialization. We can improve this situation by Lazy Loading component in the application.

What is Lazy Loading?

Lazy Loading is a process of loading only those components which are required for the current view. Applications using Lazy Load do not load all components at once so at initialization only one component of root page will be loaded, this really improves the loading time of ionic applications.

How to Implement Lazy Loading in Existing App?

To demonstrate the implementation of Lazy loading first we will create a new blank Ionic application. After that will implement Lazy Loading on Home Component which is created by default in new Ionic 3 Project. After that, we will implement Lazy loading by doing some changes.

So let’s begin

Create New Ionic 3 Application

If you are using Visual Studio Code editor then type $ code . to open files directly from cmd.

Now we have a normal file structure with the blank template not having Lazy Load functionality.

Let’s follow following steps to convert out Ionic 3 project with Lazy Loaded Components

Step 1) Changes in App Module File

Open the app.module.ts file, then remove component imports from declarations and entryComponents except MyApp

So after these changes, app.module.ts file will look like this

 

Step 2) Create a Module file for each page/ Component

In the home, page folder create home.module.ts file with the following code in it

If you have more pages/ views, then you need to create module.ts in each page’s folder.

Note: If you generate a new page using the command   $ ionic generate page HomePage then it will automatically create the module.ts file.

Step 3) Add @IonicPage() decorator in Page Component

In HomePage component, we will add @IonicPage(), decorator, before @Component() decorator.

This will make this component Lazy Loaded. After adding home.ts file will look like as follows

 

Step 4) Convert Page components into String components.

This is an important step, you need to convert Components into the string like HomePage to ‘HomePage’

the app.component.ts file will look like this

You also need to define modules for Home Page in home.module.ts instead of app.module.ts

 

That’s all guys!!! now you will see a considerable change in app start speed on real devices. After creating production release your app size will decrease and will further improve initialization speed.

Also See: How to Create Production Release/ Signed Version of APK in Ionic 3 CMD

Leave a Reply

avatar
  Subscribe  
Notify of