Be Lazy, the Angular Way: Lazy Loading Froala in Angular

Angular speed optimization featured image

Introduction

In modern web programming, performance is very important. Users want interfaces that run quickly and give them a smooth experience. This lesson will focus on how to make Angular and Froala Editor applications run better with its angular rich text editor. We will look at how to use lazy loading to keep the Froala Editor from loading until it is needed.

Angular speed optimization banner

Prerequisites

Setting Up The Angular Project

If you don’t already have an Angular project set up, the Angular CLI makes it easy to make one:

npm install -g @angular/cli

ng new my-app

cd my-app

Next, install the angular-froala-wysiwyg package:

npm install angular-froala-wysiwyg --save

For the sake of time, we’ll skip the integration part but you can check out how to fully integrate Froala and Angular here.

Traditional Eager Loading of Froala

Let’s look at how the Froala Editor is usually loaded before we talk about lazy loading.

In your app.module.ts, import Froala:

import { FroalaEditorModule, FroalaViewModule } from ‘angular-froala-wysiwyg’;

And add it to your NgModule imports:

@NgModule({
  imports: [FroalaEditorModule.forRoot(), FroalaViewModule.forRoot() ... ],
})

Introducing Lazy Loading

Lazy loading is a way to put off jobs that take a lot of time or resources until they are needed. This can make a big difference in how long your application takes to load the first time and how well it works generally.

Lazy Loading Froala Editor using Angular Routes

Create a new module:

ng generate module froala-editor

Move the Froala imports to this new module:

In froala-editor.module.ts:

import { FroalaEditorModule, FroalaViewModule } from 'angular-froala-wysiwyg';
@NgModule({
  imports: [FroalaEditorModule.forRoot(), FroalaViewModule.forRoot() ... ],
})

Define a lazy-loaded route:

In your app-routing.module.ts:

const routes: Routes = [
  { path: 'editor', loadChildren: () => import('./froala-editor/froala-editor.module').then(m => m.FroalaEditorModule) }
];

Now, the Froala Editor will only be loaded when you navigate to the /editor route.

Checking for Lazy Loading

If you want to know if Froala Editor is lazy-loaded, look at the Network tab in the developer tools of your browser. You should see a different chunk load when you first go to the /editor route.

Additional Benefits of Lazy Loading

Beyond initial load time, lazy loading also offers other benefits:

Reduced Memory Usage: By not loading modules until you need them, you can make your program use less memory.

Improved User Experience: The time-to-interactive measure will go up because users will be able to start using the most important parts of your app sooner.

Advanced Lazy Loading Techniques

Prefetching

With prefetching, you can load modules in the background while the user isn’t using the feature. This makes sure that the feature is always there when the user needs it.

Angular provides built-in support for this via the PreloadAllModules strategy:

import { PreloadAllModules } from '@angular/router';
@NgModule({
  imports: [RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })],
  exports: [RouterModule],
})
export class AppRoutingModule { }

Conditional Lazy Loading

You can also choose to load modules if certain conditions are met. For example, you might only want admin people to be able to use the Froala Editor:

const routes: Routes = [
  { 
    path: 'editor',
    loadChildren: () => {
      if (isAdminUser()) {
        return import('./froala-editor/froala-editor.module').then(m => m.FroalaEditorModule);
      } else {
        return import('./read-only-editor/read-only-editor.module').then(m => m.ReadOnlyEditorModule);
      }
    }
  }
];

Monitoring Performance

After adding lazy loading to your Angular and Froala project, make sure to monitor its effects. This is important for finding issues and confirming that lazy loading actually improves performance.

Google Lighthouse

Use Google Lighthouse to see how lazy loading speeds up your app. Pay attention to key indicators like First Contentful Paint and Time-to-Interact.

Angular Profiler

Use Angular DevTools to create a profile of your app. This shows how lazy loading affects the memory and other resources.

Server-side Logging

When you open the Froala Editor, use monitoring tools like Grafana and Kibana to check the server’s performance. You can also link these tools to the main systems to see how they use resources.

Best Practices

Partitioning by Module: Divide your modules logically. Having a single feature or logical grouping per module simplifies the implementation of lazy loading.

Common Mistakes to Avoid: Make sure you’re not lazy-loading features that are needed for the application to work. If you do, the user experience could be bad.

Use a load-spinning wheel: Show a loading spinner when a user goes to a feature that is lazy-loading, so they know something is happening.

Check on different networks: Always check how lazy loading works on your app by using different internet speeds.

Conclusion

Lazy loading makes your app faster and is a must-have for new online apps. It helps your Angular apps work quickly and easily change as they get bigger. You can easily set up lazy loading in Angular. This saves time and doesn’t use a lot of computer power. So, lazy loading is very important for a successful Angular app.

Froala Blog Call To Action

Posted on September 22, 2023

Carl Cruz

Product Marketing Manager for Froala. A technical enthusiast at heart.

No comment yet, add your voice below!


Add a Comment

Your email address will not be published.

    Hide Show