{"componentChunkName":"component---src-templates-post-js","path":"/blog/2018/06/06/angular-preloading/","result":{"data":{"post":{"html":"<p><a href=\"https://angular.io/api/router/Router\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Angular Router</a> has been supporting lazy loading of child modules for a long time. What’s even more cool is that later on they added <code class=\"language-text\">PreloadAllModules</code> strategy so you can preload all of the modules in the background asynchronously. This will help boost up the loading time and performance tremendously.</p>\n<!--more-->\n<p>To use this feature your <code class=\"language-text\">RouterModule</code> will look like this:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"52404846124572880000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`routes: Routes = [\r\n  ...\r\n]\r\n\r\n@NgModule({\r\n  imports: [RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })],\r\n  exports: [RouterModule]\r\n})\r\nexport class AppRoutingModule { }`, `52404846124572880000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"typescript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-typescript line-numbers\"><code class=\"language-typescript\">routes<span class=\"token operator\">:</span> Routes <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span>\r\n  <span class=\"token operator\">...</span>\r\n<span class=\"token punctuation\">]</span>\r\n\r\n<span class=\"token decorator\"><span class=\"token at operator\">@</span><span class=\"token function\">NgModule</span></span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\r\n  imports<span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>RouterModule<span class=\"token punctuation\">.</span><span class=\"token function\">forRoot</span><span class=\"token punctuation\">(</span>routes<span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span> preloadingStrategy<span class=\"token operator\">:</span> PreloadAllModules <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\r\n  exports<span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>RouterModule<span class=\"token punctuation\">]</span>\r\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\r\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">AppRoutingModule</span> <span class=\"token punctuation\">{</span> <span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"implementing-your-own-preloading-strategy\" style=\"position:relative;\"><a href=\"#implementing-your-own-preloading-strategy\" aria-label=\"implementing your own preloading strategy permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Implementing your own preloading strategy</h2>\n<p>The above feature is great, however, in real world and most of the times the application is too big and loading all the modules in the background is just adding extra traffic while in reality users might not navigate to all of your child routes. <a href=\"https://angular.io/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Angular</a> does not have any other predefined strategy, but it allows you to customise the preloading by implementing its <code class=\"language-text\">PreloadingStrategy</code> class.</p>\n<p>This will allow you to preload your core modules, the ones your users use the most, and let the other modules be loaded on demand. Reminds me of that old saying:</p>\n<blockquote>\n<p>Moderation in all the things is the best policy.</p>\n</blockquote>\n<p>So let’s get implementing 👨‍💻.</p>\n<h3 id=\"1--add-a-flag-to-routes\" style=\"position:relative;\"><a href=\"#1--add-a-flag-to-routes\" aria-label=\"1  add a flag to routes permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1- Add a flag to routes</h3>\n<p>First we need to add a flag to our routes so that we know which ones to preload and which to not. We assume we have four routes called <code class=\"language-text\">Home</code>, <code class=\"language-text\">Shop</code>, <code class=\"language-text\">About</code>, and <code class=\"language-text\">Contact</code> from which the first two are the most frequently visited routes.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"47752593387913535000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`routes: Routes = [\r\n  {\r\n    path: '',\r\n    redirectTo: 'home',\r\n    pathMatch: 'full',\r\n  },\r\n  {\r\n    path: 'home',\r\n    component: HomeComponent,\r\n  },\r\n  {\r\n    path: 'shop',\r\n    loadChildren: './shop/shop.module#ShopModule',\r\n    data: { preload: true }, // flag we will use to track what route to be preloaded\r\n  },\r\n  {\r\n    path: 'about',\r\n    loadChildren: './about/about.module#AboutModule',\r\n  },\r\n  {\r\n    path: 'contact',\r\n    loadChildren: './contact/contact.module#ContactModule',\r\n  },\r\n]`, `47752593387913535000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"typescript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-typescript line-numbers\"><code class=\"language-typescript\">routes<span class=\"token operator\">:</span> Routes <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span>\r\n  <span class=\"token punctuation\">{</span>\r\n    path<span class=\"token operator\">:</span> <span class=\"token string\">''</span><span class=\"token punctuation\">,</span>\r\n    redirectTo<span class=\"token operator\">:</span> <span class=\"token string\">'home'</span><span class=\"token punctuation\">,</span>\r\n    pathMatch<span class=\"token operator\">:</span> <span class=\"token string\">'full'</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token punctuation\">{</span>\r\n    path<span class=\"token operator\">:</span> <span class=\"token string\">'home'</span><span class=\"token punctuation\">,</span>\r\n    component<span class=\"token operator\">:</span> HomeComponent<span class=\"token punctuation\">,</span>\r\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token punctuation\">{</span>\r\n    path<span class=\"token operator\">:</span> <span class=\"token string\">'shop'</span><span class=\"token punctuation\">,</span>\r\n    loadChildren<span class=\"token operator\">:</span> <span class=\"token string\">'./shop/shop.module#ShopModule'</span><span class=\"token punctuation\">,</span>\r\n    data<span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> preload<span class=\"token operator\">:</span> <span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> <span class=\"token comment\">// flag we will use to track what route to be preloaded</span>\r\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token punctuation\">{</span>\r\n    path<span class=\"token operator\">:</span> <span class=\"token string\">'about'</span><span class=\"token punctuation\">,</span>\r\n    loadChildren<span class=\"token operator\">:</span> <span class=\"token string\">'./about/about.module#AboutModule'</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token punctuation\">{</span>\r\n    path<span class=\"token operator\">:</span> <span class=\"token string\">'contact'</span><span class=\"token punctuation\">,</span>\r\n    loadChildren<span class=\"token operator\">:</span> <span class=\"token string\">'./contact/contact.module#ContactModule'</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n<span class=\"token punctuation\">]</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<div class=\"custom-block info\"><div class=\"custom-block-body\"><strong>Note</strong> The data property is reserved by the router to allow developers to add their custom values to their route configs.</div></div>\n<p>As you can see we used the data property to be able to define our flag. This will get used in our strategy later.</p>\n<h3 id=\"2--create-our-custom-strategy\" style=\"position:relative;\"><a href=\"#2--create-our-custom-strategy\" aria-label=\"2  create our custom strategy permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>2- Create our custom strategy</h3>\n<p>Ok now it’s time to create our custom strategy:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"53177505182474840000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`import { PreloadingStrategy, Route } from '@angular/router'\r\n\r\nimport { Observable, of } from 'rxjs'\r\n\r\nexport class CustomPreloadingStrategy implements PreloadingStrategy {\r\n  preload(route: Route, load: Function): Observable<any> {\r\n    return route.data && route.data.preload ? load() : of(null)\r\n  }\r\n}`, `53177505182474840000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"typescript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-typescript line-numbers\"><code class=\"language-typescript\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> PreloadingStrategy<span class=\"token punctuation\">,</span> Route <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@angular/router'</span>\r\n\r\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> Observable<span class=\"token punctuation\">,</span> <span class=\"token keyword\">of</span> <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'rxjs'</span>\r\n\r\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">CustomPreloadingStrategy</span> <span class=\"token keyword\">implements</span> <span class=\"token class-name\">PreloadingStrategy</span> <span class=\"token punctuation\">{</span>\r\n  <span class=\"token function\">preload</span><span class=\"token punctuation\">(</span>route<span class=\"token operator\">:</span> Route<span class=\"token punctuation\">,</span> load<span class=\"token operator\">:</span> <span class=\"token builtin\">Function</span><span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> Observable<span class=\"token operator\">&lt;</span><span class=\"token builtin\">any</span><span class=\"token operator\">></span> <span class=\"token punctuation\">{</span>\r\n    <span class=\"token keyword\">return</span> route<span class=\"token punctuation\">.</span>data <span class=\"token operator\">&amp;&amp;</span> route<span class=\"token punctuation\">.</span>data<span class=\"token punctuation\">.</span>preload <span class=\"token operator\">?</span> <span class=\"token function\">load</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">:</span> <span class=\"token keyword\">of</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">null</span><span class=\"token punctuation\">)</span>\r\n  <span class=\"token punctuation\">}</span>\r\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>What is happening is that we just check the flag we added and preload the route if that is set to true. A preloading strategy expects a class with a method <code class=\"language-text\">preload()</code>. The preload method should return an <code class=\"language-text\">Observable</code> calling the load parameter or an <code class=\"language-text\">Observable</code> of null. Now that we have the strategy defined lets use it.</p>\n<h3 id=\"3--use-the-custom-strategy-in-our-routing-module\" style=\"position:relative;\"><a href=\"#3--use-the-custom-strategy-in-our-routing-module\" aria-label=\"3  use the custom strategy in our routing module permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>3- Use the custom strategy in our routing module</h3>\n<p>Now that we have the basics lets use this custom strategy in our routing module:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"22770736009662970000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`import { NgModule } from '@angular/core'\r\nimport { Routes, RouterModule } from '@angular/router'\r\n\r\nimport { HomeComponent } from './home/home.component'\r\nimport { CustomPreloadingStrategy } from './custom-preloading-strategy'\r\n\r\nroutes: Routes = [\r\n  {\r\n    path: '',\r\n    redirectTo: 'home',\r\n    pathMatch: 'full',\r\n  },\r\n  {\r\n    path: 'home',\r\n    component: HomeComponent,\r\n  },\r\n  {\r\n    path: 'shop',\r\n    loadChildren: './shop/shop.module#ShopModule',\r\n    data: { preload: true }, // flag we will use to track what route to be preloaded\r\n  },\r\n  {\r\n    path: 'about',\r\n    loadChildren: './about/about.module#AboutModule',\r\n  },\r\n  {\r\n    path: 'contact',\r\n    loadChildren: './contact/contact.module#ContactModule',\r\n  },\r\n]\r\n\r\n@NgModule({\r\n  imports: [\r\n    RouterModule.forRoot(routes, {\r\n      preloadingStrategy: CustomPreloadingStrategy,\r\n    }),\r\n  ], // Using our own custom preloader\r\n  exports: [RouterModule],\r\n  providers: [CustomPreloadingStrategy],\r\n})\r\nexport class AppRoutingModule {}`, `22770736009662970000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"typescript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-typescript line-numbers\"><code class=\"language-typescript\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> NgModule <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@angular/core'</span>\r\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> Routes<span class=\"token punctuation\">,</span> RouterModule <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@angular/router'</span>\r\n\r\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> HomeComponent <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'./home/home.component'</span>\r\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> CustomPreloadingStrategy <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'./custom-preloading-strategy'</span>\r\n\r\nroutes<span class=\"token operator\">:</span> Routes <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span>\r\n  <span class=\"token punctuation\">{</span>\r\n    path<span class=\"token operator\">:</span> <span class=\"token string\">''</span><span class=\"token punctuation\">,</span>\r\n    redirectTo<span class=\"token operator\">:</span> <span class=\"token string\">'home'</span><span class=\"token punctuation\">,</span>\r\n    pathMatch<span class=\"token operator\">:</span> <span class=\"token string\">'full'</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token punctuation\">{</span>\r\n    path<span class=\"token operator\">:</span> <span class=\"token string\">'home'</span><span class=\"token punctuation\">,</span>\r\n    component<span class=\"token operator\">:</span> HomeComponent<span class=\"token punctuation\">,</span>\r\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token punctuation\">{</span>\r\n    path<span class=\"token operator\">:</span> <span class=\"token string\">'shop'</span><span class=\"token punctuation\">,</span>\r\n    loadChildren<span class=\"token operator\">:</span> <span class=\"token string\">'./shop/shop.module#ShopModule'</span><span class=\"token punctuation\">,</span>\r\n    data<span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> preload<span class=\"token operator\">:</span> <span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> <span class=\"token comment\">// flag we will use to track what route to be preloaded</span>\r\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token punctuation\">{</span>\r\n    path<span class=\"token operator\">:</span> <span class=\"token string\">'about'</span><span class=\"token punctuation\">,</span>\r\n    loadChildren<span class=\"token operator\">:</span> <span class=\"token string\">'./about/about.module#AboutModule'</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token punctuation\">{</span>\r\n    path<span class=\"token operator\">:</span> <span class=\"token string\">'contact'</span><span class=\"token punctuation\">,</span>\r\n    loadChildren<span class=\"token operator\">:</span> <span class=\"token string\">'./contact/contact.module#ContactModule'</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\r\n<span class=\"token punctuation\">]</span>\r\n\r\n<span class=\"token decorator\"><span class=\"token at operator\">@</span><span class=\"token function\">NgModule</span></span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\r\n  imports<span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\r\n    RouterModule<span class=\"token punctuation\">.</span><span class=\"token function\">forRoot</span><span class=\"token punctuation\">(</span>routes<span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span>\r\n      preloadingStrategy<span class=\"token operator\">:</span> CustomPreloadingStrategy<span class=\"token punctuation\">,</span>\r\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\r\n  <span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> <span class=\"token comment\">// Using our own custom preloader</span>\r\n  exports<span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>RouterModule<span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\r\n  providers<span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>CustomPreloadingStrategy<span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\r\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\r\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">AppRoutingModule</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>And that’s it. Now if you run your application, the home component will load within the main bundle, shop component will load asynchronously in the background. It means that by the time user clicks the shop from the nav menu, it’s already loaded and it feels super fast.</p>\n<p>If user clicks on any of the about or contact, however, there would be another script loaded containing that component and you will need to handle the waiting time by showing a spinner or something. I am joking, I hate spinners, I will write another blog post soon on how to improve the performance so you won’t need a spinner.</p>\n<p>Hope this helps to increase your applications performance and make you curious enough to dive deeper in Angular and its awesome new features.</p>\n<p>Until next post, adiós.</p>","timeToRead":3,"frontmatter":{"unformattedDate":"2018-06-06T00:00:00.000Z","date":"June 06, 2018","title":"Boost up your performance using Angular preloading strategy","subtitle":null,"id":null,"author":"Yaser Adel Mehraban","gravatar":null,"canonical_url":null,"tags":["angular","router","preloading strategy","lazy loading"],"thumbnail":{"childImageSharp":{"gatsbyImageData":{"layout":"constrained","placeholder":{"fallback":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsSAAALEgHS3X78AAAB8UlEQVR42o2SXWvTUBjHz6riQFSGCoIfQLwRhvhyuy/gjYR54agMLc630Q7dcM6KDEVB1kLcUKzJlPai6Dad80IKdXMOZqyuCxUrLGkcq5a0S6uu7XqS/M1SdiHd0AOHh3P+5/md542QfywQUrdiy8BefGC7MHa6D8lhVwHYYetA3frOVXHD6nYdIJvse2X8JAKHSvBvB4JNBu5uBp41S8tAo617vY6/QAzDrACIx+NpZVlWDoWCM6HgE3FwkBdHhodE49HBcuVWA9KjPXR6apxKnJOaNxzAyxZRBbbWRBoOh22g2+3u1DQNBqDL81moS9YzJQJ0E2Mh4jdnsxXkChqEORVvrh7R0bcbSE8eqwKrjBpgPq9hWkzTkddxcyr524TQbxrXCKQvn7BQLCL6YgjZnIaxwHWqdDWgMuG/YAOj3o01KTudzkvx+AzUbK4sp+ZpplCiuXccxRVCldmo+c3qTCa/CLlkZXv7ROXr5Z0oSm+ZdSMUBKEzkUiA53m9/eJ5cBwHSZ4DnjZB7d1jRp4/pu8/xujog57lmMuBH/zxz1ZRttXUcPVg2UZd19t9Pt8Zhmluuz9w7+xPHaeQmriJh/t/lXrrETtXbyQ7tiA1cFRZKnw/vGaX/2cOTWAfhDvdxVcdbGYy0GZ9vmutOfwDA8VQOLsDyGEAAAAASUVORK5CYII="},"backgroundColor":"transparent","images":{"fallback":{"src":"/static/28f2ac81793762d853d3989d7635cb56/bb185/preloading.png","srcSet":"/static/28f2ac81793762d853d3989d7635cb56/bb185/preloading.png 750w","sizes":"(min-width: 750px) 750px, 100vw"},"sources":[{"srcSet":"/static/28f2ac81793762d853d3989d7635cb56/e70a9/preloading.webp 750w","type":"image/webp","sizes":"(min-width: 750px) 750px, 100vw"}]},"width":750,"height":331}}},"cover":{"childImageSharp":{"gatsbyImageData":{"layout":"constrained","placeholder":{"fallback":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAIAAAA7N+mxAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB9klEQVR42h2N2W7aQBSG/dRVn6HXlXLRV+hFl7RJSZqUsoyBMWAIeN+wjdcZb2MzNmB1qPTp6NM5+s/PbRwsh0SNGyWuLdQc8MnFpwO+iY1ODqJ+ToOCHouWSVS2ftG6GfXZNS454Viqabu0k41fbP1cCyszIQ5ujLjSIgYx4vr2KKnclNhxoYeFFhRp03u44YBfwIB8GwkvK+UZSg+T9QCsfg7nX54njMe/8/tX/oltRvCPsH+ZbwYTOFpJAbk6KQt7OUxOIr4o+GJlZzPr+L05XMrDpfIKdyNRG4nq00wcv5lO1R+qi0euLrmq+CyFNWvOhahZ2PnCzFaHchfUZnb26/5Y99GpD+reZZDeLs4GbnXcWlmrIiqj7i2oOGAkgpkvNAytQnBKwamWdrk5lEp00hOqp62aUDmlMu4U3N1m1smoZb4NCAfkaGVlawNBM5+q8VSJgZos7GrhVPAGgQcC/RrGVEAdjLqx3goB3aNue6w43sjmE7AefIV6+gCk++nuEWrQY5kKuuQm7g32S0TN9+H5/bvL5wndlO3cKzloFz9+g7uPdzMjnZtoZmPeQiwjuETw/sOENfuNGNW/IP3w6TzYURG3UzPjxm/eVEmGUszrKdASYKS8nfFqxMsBr4S8xGYETAQszJhZCXTTmYOBgUdS+A/3oQrYhuXrkgAAAABJRU5ErkJggg=="},"backgroundColor":"transparent","images":{"fallback":{"src":"/static/0cd5af469074266aef3d3733d255bd0c/a2195/socialpreview.png","srcSet":"/static/0cd5af469074266aef3d3733d255bd0c/cadb4/socialpreview.png 750w,\n/static/0cd5af469074266aef3d3733d255bd0c/a2195/socialpreview.png 800w","sizes":"(min-width: 800px) 800px, 100vw"},"sources":[{"srcSet":"/static/0cd5af469074266aef3d3733d255bd0c/11ba1/socialpreview.webp 750w,\n/static/0cd5af469074266aef3d3733d255bd0c/5f042/socialpreview.webp 800w","type":"image/webp","sizes":"(min-width: 800px) 800px, 100vw"}]},"width":800,"height":400}}},"img":{"publicURL":"/static/28f2ac81793762d853d3989d7635cb56/preloading.png"},"socialPreview":{"publicURL":"/static/0cd5af469074266aef3d3733d255bd0c/socialpreview.png"}}}},"pageContext":{"title":"Boost up your performance using Angular preloading strategy","postPath":"/blog/2018/06/06/angular-preloading","nextPost":"/blog/2018/07/28/angular-react","previousPost":"/blog/2018/06/06/blog-migration"}},"staticQueryHashes":[],"slicesMap":{}}