Understanding Angular 8's Differential Loading

Understanding Angular 8's Differential Loading

Learn all about one of its coolest new features: differential loading. Differential loading lets you serve up different bundles to different browsers and make your application even faster!

In this article, I want to dive into that last one: differential loading. What is that? Why does it matter? What do I need to do about it (if anything)?

Angular 8 now sends separate bundles to legacy browsers by default.

Angular 8 has only been out for about a week at the time I’m writing this, but there’s already been 17,000 “What’s New” articles published. Rather than throw my own take on the pile, I’ll refer you to the official Angular release announcement but here are the high points:

  • No, Ivy isn’t ready yet (it’s an opt-in preview).
  • No, Bazel isn’t ready yet (it’s an opt-in preview).
  • Builders allow you to extend and customize the CLI. For example, you’re now able to deploy to Firebase and other providers from the CLI.
  • There’s improved support for web workers, such as the ability to generate them from the CLI and use them in your application.
  • Rather than using the “magic string” syntax specific to Angular to do lazy loading, you’ll be able to use the standard [import()](https://next.angular.io/guide/deprecations#loadchildren-string-syntax) syntax. You can even perform this automatically for your app with the [angular-lazy-routes-fix](https://github.com/phenomnomnominal/angular-lazy-routes-fix) tool.
  • The new unified location service improves migration from the AngularJS $location service.
  • The Angular team has created a simplified Getting Started Guide.
  • There’s a new Deprecation Guide to assist users with updating Angular.
  • Differential loading is turned on in the CLI by default.
What is Differential Loading?

Like most buzzwords in tech, the term “differential loading” is completely opaque about what it actually means. In a nutshell, differential loading means to send newer, flashier code to newer browsers and stable legacy code to legacy browsers. This generally translates into two things: modern syntax and polyfills. On a newer browser that supports recent syntax changes in JavaScript (such as arrow functions in ES2015 or async functions in ES2017), it’d be great to ship to code as-is with as few polyfills as possible to keep download time low. On legacy browsers, we’ll need both transpiled code and more polyfills to make things work. This way, newer browsers aren’t punished for legacy browsers by having to load a massive bundle, but legacy browsers aren't left in the dust.

Sounds cool, right? How does this work? Modern browsers have the ability to interpret a module type in the script HTML tag and to ignore a nomodule attribute. It looks like this:



When a modern browser (like a new version of Chrome) runs across this, it will know to only load fancyModernBundle. Likewise, when an older browser sees this, it won’t recognize the module type and only load legacyBundle.

How You Normally Set Up Differential Loading

You’ve probably surmised by now that, in order to take advantage of differential loading, you’ll need to divide up your JavaScript code based on browser support. “Easy,” you think, “I’ll just plug the transpiled code into the nomodule tag and the original into the module tag.” Well, it’s not quite that simple. Unfortunately, not all browsers support all the same features of JavaScript. Beyond that, your users may not use the same browser you do. In fact, if your users are around the world, they may not even use browsers you’ve heard of — Opera Mini has over 100 million users in Africa!

It turns out that splitting up your bundle for differential loading is a combination of art and science. To pull this off, your bundler (like Webpack) and your transpiler or compiler (like Babel or TypeScript) must work together to divide up your code and add the correct polyfills in the right places. Luckily, there’s a configuration tool called [browserslist](https://github.com/browserslist/browserslist) that does nearly all of the heavy lifting for you.

Browserslist works with tools like Babel and is essentially a configuration tool that accepts query strings, which it runs against Can I Use data. Tools use Browserslist through either an entry in package.json:

// package.json
"browserslist": [
  "last 1 version",
  "> 1%",
  "maintained node versions",
  "not dead"
]

Or in a separate config file called .browserslistrc:

// .browserslistrc
# Browsers that we support

last 1 version
> 1%
maintained node versions
not dead

Both of these formats contain the same query:

  • last one version: the last version for each browser
  • >1%: browsers versions selected by global usage statistics
  • maintained node versions: all Node.js versions, which are still maintained by Node.js Foundation
  • not dead: excludes "dead" browsers, which are the same as those from a last 2 versions query, but with less than 0.5% in global usage statistics and without official support or updates for 24 months

Browserslist also has a defaults query that works for many people. They’ve got great explanations of the queries on their GitHub repo and there’s even a site called browserl.ist that lets you plug in a query and see a visual list of supported browsers.

Differential Loading in Angular 8

Ordinarily, you’d need to configure your bundler and Babel or TypeScript to use browserslist to split up your code into the correct bundles. With version 8 of the Angular CLI, you actually get this set up right out of the gate with nothing for you to do! This is awesome because differential loading saves on average 7-20% in bundle size for Angular applications in modern browsers. In fact, angular.io saved over 40kB of its initial bundle size for modern browsers when the team enabled differential loading.

Updating to Angular 8

First, make sure you have the latest version of the Angular CLI installed:

npm install -g @angular/cli

If you’d like to try this on an existing project, you can update it to Angular 8 by running:

ng update @angular/cli @angular/core

Check out the Angular Update Guide if your app requires more heavy lifting to update, like if it’s currently in version 6 or lower.

Otherwise, you can create a new application:

ng new diff-loading

Follow the prompts about routing and styling and then you’re good to go!

Configuration and building

If you open the new project in your favorite editor, you’ll notice a new file called browserslist. It contains the following by default:

// browserslist
> 0.5%
last 2 versions
Firefox ESR
not dead
not IE 9-11 # For IE 9-11 support, remove 'not'.

You can adjust this list to match better with your own user demographics.

One very important note about this: See that not IE 9-11 part? If you need to support those browsers, you can remove the not, but it comes at a price. Those versions of Internet Explorer can’t take advantage of differential loading, so it will be disabled.

If you open tsconfig.json, you’ll see a particularly big change in the target property: it’s set to es2015 by default instead of es5.

Now that you’ve seen where the configuration lives, build the application with this command:

ng build --prod

After it’s done, open up dist/index.html and notice the script tags:







See all those module and nomodule instances? The Angular CLI did all that for you!

Differential Loading in Angular, in action

Now you just need to see it in action. Install a simple server like http-server:

npm install -g http-server

Then, within your project folder, serve up the project inside the dist folder:

http-server dist/diff-loading/

Open Chrome or another modern browser and head to the address of the server (on my machine it’s [http://192.168.0.4:8080](http://192.168.0.4:8080) but it may be different for you). Open up the network tab of the developer tools and refresh the page. You should see only the scripts that include es2015 in the name!

Other settings

One quick note before I end this tutorial. If you change the different configurations, there are four different scenarios you could end up with:

  • A single ES5 build (differential loading disabled, target is es5)
  • Single ES5 build with conditional polyfills (differential loading enabled, target is es5)
  • Single ES2015 build (differential loading disabled, target is es2015)
  • Two builds with conditional polyfills (differential loading enabled, target is es2015)

To read more, check out the official documentation.

Conclusion

Angular 8 brings lots of great new stuff to Angular, including differential loading. I hope you’ve enjoyed this look into what differential loading is, how it works, and how to use it in Angular 8.

Mobile App Development Company India | Ecommerce Web Development Company India

Mobile App Development Company India | Ecommerce Web Development Company India

Best Mobile App Development Company India, WebClues Global is one of the leading web and mobile app development company. Our team offers complete IT solutions including Cross-Platform App Development, CMS & E-Commerce, and UI/UX Design.

We are custom eCommerce Development Company working with all types of industry verticals and providing them end-to-end solutions for their eCommerce store development.

Know more about Top E-Commerce Web Development Company

Web Development Services

Web Development Services

As one of the best Web Application Development Company, it provides a fine quality mobile app development service at an affordable price. Especially, it encourage start-ups that have unique ideas, by offering a more competitive price

HireFullStackDeveloperIndia is rated as one of the top Web Application Development Company in India by various industry magazines and review sites. They have a right blend of award-winning designers, expert programmers and Google certified digital marketers which make them a unique one-stop solution for hundreds of our clients, spread across all countries.

A Good website reflects not only your business but also it is one of the main factors why a potential customer would convert into Client. A good website design helps increase traffic driving leads to grow business. The best web design company create a custom design for each corporate website so as to help them meet their business goals.

Get Quote: https://hirefullstackdeveloperindia.com/get-a-quote/

Top Web Application Developer

Top Web Application Developer

You can also contact a web application development company for your business but then why not to contact the best web application development company that can turn up your business and customer satisfaction ratio to sky touching heights.

Not long-ago internet came into existence and the world has never been the same ever since. The Internet made sure that people and business do evolve at a faster rate than ever and was never merciful to the slow ones. Because of this competition and availability of any business with few clicks made India one of the hubs as IT center. This trend of constantly updating has given rise to smartphones, smart machines, wearable gadgets and a lot more is yet to come in the upcoming years. In such time it is always a good idea to hire Web App Developer from India at your service. They are expert in developing not only websites but web applications as well.

We at HireFullStackDeveloperIndia, have a huge team of experienced developers that have grasped over different domains in front and back end development. You can hire web app developers in India from us with many advantages that you won’t get anywhere else in the industry.

Here is why we are the best option if you are looking forward to hiring web app developers in India:

  • Flexible hiring models available, as per your convenience and requirement
  • Maximum ROI, compared to any other company or team of developers.
  • We provide you with Source code Authorization meaning code written for you belong only to you. It cannot be used or copied anywhere else.
  • All of our developers are sound with agile development methodology, so you will be in a constant loop of suggestions, ideas, trends, and updates about your project.
  • Our developers are good with creating custom web applications as well; it guarantees you a better product without any compromise of non-existing functionalities.
  • You can save huge costing on infrastructure by utilizing our hire web app developer in India program.
  • Your hired developer or team will be easily accessible to your preferred mode of communication.
  • You get to exercise complete control over your team or individual that you hire.
  • We believe in Integrity and Transparency.
  • Our developers are highly creative and motivated to deliver excellence.


HireFullStackDeveloperIndia is a Web Application Development Company in India that is known worldwide for our Innovative guaranteed solutions. You can inquire with us about your project and we will be providing you multiple suitable developers that are the best fit for your requirements. You can evaluate them and select one or multiple whosoever deems fit to you. After this, all you have to do is provide your valuable input to the resource through sprint base project development until you get delivery of your project.

Our engagement model will also allow you to get our development team to your site location and proceed with development from your premises.