×

Deployment of an Angular 8 app

Deploying an Angular 8 app

To implement our application, we have to compile it and then host the JavaScript, CSS, and HTML on a web server. Build an Angular application which is very portable and can live in any environment or served by any technology such as Node, java, .net, PHP, and many others.

When we are ready to deploy our angular application to a remote server, we have various option for deployment.

Deploying an Angular 8 app

Simple Deployment Options

Before fully deploying our application, we can test the process, build configuration, and deployed behavior by using one of these techniques:

Building and serving from the disk

During development, we typically use the ng serve command to build, watch, and serve the application from local memory, using webpack dev server. When we are ready to deploy; however, we must use the ngmodule command to build the app and deploy the build, we must generate the system originated in the methodology.

Both ng build, and ng serve clear the output folder before it creates a project. But only the ng build command is used to build the app and deploy the build artifacts anywhere.

NOTE: The output folder is dist/project name by default. To output, a different folder, change the output path in angular JSON.

We will need two terminal to get the live-reload experience.

  • On the first terminal, run the ng build command in watch mode to compile the application to the dist folder.
ng build --watch

Like the ng serve command, this regenerates output files when source files change.

  • On the second terminal, install a web server, and run it against the output folder. For example:
lite-server –baseDir=”dist”  

             The server will automatically reload our browser when new files are output.

NOTE: This method is for development and testing only, and is not supported or secure way of deploying an application.

Deployment Preparation

Requesting services from a different server (CORS)

Angular developers may encounter a cross-origin resource sharing error when making a service request ( a data service request) to a server other than the application's host server. Browser forbids such claims unless the server permits them explicitly.

There is not anything the client application can do about these errors. The server must be configured to accept the application’s requests.

Production optimizations

The –prod meta-flag engages the following build optimization features.

  • Ahead-of-time (AOT) compilation: AOT compilation ispre-combines Angular component templates.
  • Production mode: deploys the production environment, which enables the production mode.
  • Bundling: Itconcatenates our many application and library files into a few bundles.
  • Minification: It is used to removes excess whitespace, comments, and optional tokens.
  • Uglification: It rewrites code to use short, cryptic variable, and function names.
  • Dead code elimination: removes unreferenced modules and much-unused code.

Measure performance

We can make better decisions about what to optimize and how when we have a clear and accurate understanding of making the application slow.

We waste a lot of time and money optimizing something that has no tangible benefit or even makes the app slower. We should measure the app's actual behavior when running in an environment that is important to us.

The WebChromeTest tool is another good choice that can also help verify that our deployment was successful.

Inspect the bundles

The source-map-explorer tool is a great way to inspect the generated JavaScript bundles after a production build.

Install source-map-explorer:

npm install source-map-explorer –save-dev 

Build our app for production including the source maps

ng build –prod –source-map

List the created bundles in the dist/folder.

ls dist/*.bundle.js

Run the explorer to generate a graphical representation of one of the bundles. The following example displays the graph for the main bundle.

node_module/bin/source-map-explorer dist/.main.*.bundle.js

The source-map-explorer analyze the source map generated with the bundle and draws the map of all dependencies, showing which class is included in the package.

The output for the main bundle is called cli-quick start.

main bundle is called cli-quick start

Differential loading

When we are building web applications, making sure that our app is compatible with the majority of browsers in a goal. Even JavaScript continues to evolve, with new features introduced, not all browsers are updated with support of these new features at the same pace.

The code we write in development using TypeScript is compiled and bundled into ES2015, the JavaScript syntax that is compatible with most browsers.

To maximize compatibility, we could ship a single bundle that includes all our compiled code, any polyfills that may be needed. Users with modern browsers should not have to pay the price of increased bundle size that comes with polyfills which can bridge the gap to providing functionality that doesn't exist in older versions of JavaScript supported by the browser.

To maximize compatibility, you could ship a single bundle that includes all your compiled code, plus any polyfills that may be needed.

Differential loading is a strategy where CLI builds two separate bundles of the deployed apps.

  • The first bundle contains modern ES2015 syntax, takes advantage of built-in support in modern browsers, ships fewer polyfills, and results in smaller bundle size.
  • The second bundle contains code in the old ES5 syntax, along with all necessary polyfills. This results in a large bundle size but supports older browsers.

This strategy allows us to continue to build our web application supporting multiple browsers, but only load the necessary code that the browser needs.


Related Topics

Angular 8 App Loading

How an Angular 8 app loaded and started When we create an Angular app and run it by using ng serve command, it looks like the below screenshot. It is a simple...

3 minutes read.

Event Binding in Angular 8

Event binding in Angular 8 Event binding is a data-binding from an element to a component. User actions such as keystrokes, mouse movement, clicks, and touches can be bound to the component property...

1 minute read.

Angular 8 ngClass Directive

Angular 8 ngClass Directive ngClass Directive is a type of attribute directive. Angular provided built-in directive, and it helps in adding or removing CSS classes on an HTML element. The ngClass directive allows...

2 minutes read.

History and Versions of Angular 8

Introduction to Angular JS Misko created Angular JS and the first version of Angular also name as "Angular 1". He built a framework to handle the downfalls of HTML. The first...

3 minutes read.

How to create a Todo List App in Angular 7

What is a Todo List App? Todo List app is an app where users can add various tasks that they want to perform and have an idea of every task. If...

4 minutes read.

Routing in Angular 8

Routing Angular Router is a powerful JavaScript router is built and maintained by the Angular core team that can install from the package @angular/router. Routing provides a complete routing library with the possibility of multiple...

4 minutes read.

Angular 8 Libraries

Angular libraries are built a solution of general problems like presenting a unified user interface, presenting data, and allowing the data entry. Developers can create standard solutions for particular domains...

3 minutes read.

Angular 8 with Bootstrap

How to install Bootstrap for Angular? Run the following Command in Command prompt. npm install–save bootstrap@3= > The @3 is essential! After that, when we use a project created with Angular CLI 6+, we will...

1 minute read.

Creating form in Angular 8

The angular reactive form is used to handle the user's input. We can use Angular form in the application to authorize the user to log in, to update profile, to enter information,...

2 minutes read.

Express.js app.listen() Function

Express or Express.js is a flexible online application framework for Node.js that offers a strong set of functionality for both web and mobile applications. While preserving the familiar and adored...

3 minutes read.

$timeout service in AngularJS

The discipline of web development is expanding quickly. A technology that is released today will inevitably become obsolete in a few months. The webpages were static in the past and...

4 minutes read.

Creating first APP in Angular 8

Firstly, we have to open Git Bash and, then we have to write the following command in it. ng new my -app The project has been created now so that now we have to go...

2 minutes read.

Angular 8 NgStyle Directive

Angular 8 NgStyle Directive The ngStyle attribute is used to change and style the multiple properties of Angular. We can change the value, color, and size, etc. of the component. It is a built-in...

1 minute read.

Angular 8 Conclusion

Angular version 8 looks like a more accessible solution entirely focused on the modern technological trends added features. Like Ivy, route configurations use dynamic imports, builder APIs in the CLI, web worker support,...

2 minutes read.

Angular 8 *ngFor Directive

Angular 8ngFor Directive The *ngFor directive is used to repeat to repeat a portion of HTML template once per each item from an iterable list (collection). The ngFor is an Angular...

2 minutes read.

Angular 8 universal

Angular 8 universal Angular Universal is a technology that allows server-side rendering for Angular apps. An angular app is a single-page app, and it runs in a client-side browser. Rendering pages in the DOM is...

6 minutes read.

Angular 8 File Structure

File Structure in Angular 8: A file structure contains the files for one or more projects. A project is the set of files that comprise an application on a shareable...

3 minutes read.

Advantages | Disadvantages of Angular 8

Advantages of Angular 8 There are some Advantages of angular 8 which are given below: It offers clean code developmentHigher PerformanceAn angular framework can take care of routing, which...

1 minute read.

Angular 8 Architecture

Architecture of Angular 8 Angular 8 is a platform and framework which is used to create clients applications in HTML and Typescript. Angular 8 is written in Typescript. Typescript is a...

5 minutes read.

Property binding in Angular 8

Property binding is the primary way to binding data in Angular.  To bind data to a property of any element, we use square braces[ ]. It is also a one-way data-binding...

2 minutes read.