Software systems to innovate and grow

Boosting Angular’s Performance With Lazy Loading (Part 3)

By Engineering Team June 4, 2019 10 mins read

Lazy loading modules are a great feature to incorporate in your projects and in most cases, the functionality is enough. But what happens when you are using non-routable modules? As a developer, you probably already know that component templates are not always fixed and you may need to dynamically create and load components at runtime. In part 3 of 3, we’ll guide you through a step by step solution for how to load dynamically, at runtime, a predefined set of modules and components in your application, all while taking advantage of the optimization benefits of using Ahead-of-Time compilation. For an introduction to what lazy loading is and some basic entry point tips, check out part 1. To Learn more about preloading strategies check out part 2.

How to Dynamically Load a Module

For this example, you will learn to dynamically load a module and render a programmatically created component. We’ll assume this module contains several components, directives, pipes and services and that this functionality is seldom used by the users of the app. Often, the module case, containing all the logic related to a dashboard editor, is where the user can select widgets, customize them and specify where they are shown. Adding this module to the main bundle is unnecessary because all the users would be forced to load it, although most of them will probably never need the feature. To have the option to only load the module when the user clicks on a button to access that section.

Let’s get to the coding.

To define the module mentioned above, create a dynamic-component. In this case, the code is the boilerplate from the ng generate component. In the module you’ll find a static property, indicating the root component, which you will use later to inject it in the view.

dynamic.module.ts

Next, lazy load the module. Add the lazyModules array property to the app angular.json file. Angular then sends a request to Webpack instructing it to create an independent chunk, which after completion will lazy load the module. The compiler pre compiles every module defined in that array. Then it divides them into separate minified chunks, moving them to the SystemJS loader at runtime with Ahead Of Time compilation.

angular.json

Now, when you start running the npm you can see how Webpack extracts the dynamic-module, as well as its dependencies, to a separate chunk. Below is the console output.

Boosting Angular’s Performance with Lazy Loading - Part 3

Let’s load the modules. For routable modules, the router needs help from the SystemJsNgModuleLoader oader. Use it in the app by declaring AppModule:

app.module.ts

Use custom directive to create a piece of code, which is used by the loader to load the module.

To accomplish this, create a provider to define the lazy load modules map. This acts as an object, defining all the modules and their paths.

Here the InjectionToken comes in handy, acting as a factory function, which can be injected later on in the directive, returning it to the proper value.

modules-map.ts

Now comes defining the directive, where all the magic happens. What the custom directive does is it takes the module name as an input, loads the module and injects the root component, previously defined in the module to the current ViewContainerRef.

module-load.directive.ts

The last step is to include the directive in the main template and add a button to display it.

app.component.html

Browser Result

See below the first loading page.

Boosting Angular’s Performance with Lazy Loading - Part 3

After the “load dynamic” button is clicked, the dynamic-module is loaded and the dynamic component rendered.

Boosting Angular’s Performance with Lazy Loading - Part 3

Now It’s Your Turn

As this three-part article series comes to a close, we hope you enjoy trying out these features in your future coding. With the combined knowledge of the two previous articles, your lazy loading repertoire should help give you more custom control over your loading times. We recommend taking full advantage of each lazy loading scenario!