Angular 9 Server Side Rendering with Angular CLI

Angular 9 Server Side Rendering with Angular CLI

Written by Gopakumar Gopalan,
on Sunday, March 15, 2020

SPAs or Single page applications are the new normal for Web industry. Now a days you often hear jargons like PWAs (Progressive Web Apps), Lazy Loading and SSR (Server Side Rendering). The main advantage of SPAs are they don’t reload the page but changes contents by manipulating the DOM elements. This comes with a caveat, the search engine bots and link scrapers have limited ability to render javascript DOM manipulations, thus SEO and Link Scraping becomes a challenge for SPAs. Angular have a Server Side Rendering package called Angular Universal (ngUniversal) which can give SSR capabilities in your Angular project.

In earlier days, to achieve server-side rendering in Angular, you need to wire up the NgUniversal package manually to your existing Angular project. And a separate build routine need to be created to build your SSR ready app.

With Angular 9 you can make your existing Angular project to have SSR in few easy steps.

Let’s create a new project in Angular with Angular CLI. To check if you have Angular CLI installed, you may run the following command.

ng --version

If you don’t have Angular CLI installed globally you may install it with your node package manager npm or yarn.

// Using NPM 

npm install -g @angular/cli

// Or using Yarn Package Manager

yarn global add @angular/cli

Once you have Angular CLI ready in your system, you can create a new Angular project by running :

ng new my-angular-ssr && cd my-angular-ssr

When you run the command above in your projects directory, a directory named my-angular-ssr will be created and a basic angular app will be crated with all node_modules installed.

Now spin up the development server with command:

ng serve --open

A default Angular landing page will be opened. When you do a view source in your browser, you will see only a place holder app-root tag and no content will be visible. You need the content to be rendered on the server as well, so that search engine bots and link scrapers can get the contents. 

Kill the development server with Ctrl+C. To get the Server Side Rendering enabled you need to run the following command: 

ng add @nguniversal/express-engine

That’s all to enable SSR in your Angular project. Now you can run SSR enabled development server with :

// Using NPM 

npm run dev:ssr —open

// Or using Yarn Package Manager

yarn dev:ssr —open

Now when the page opens, do a view source and you will see all the contents are there within the app-root tag. Congratulations you have your SSR enabled Angular project ready.

To build the Angular project with SSR you need to use the command :

// Using NPM 

npm run build:ssr

// Or using Yarn Package Manager

yarn build:ssr

dist directory in your Angular project directory will have a directory with the name of your project, in our case it is my-angular-ssr. That directory have two sub directories one browser and other server. The server directory have main.js file which is responsible for spinning up the express based server.

When you deploy your project, take the dist directory to the application root directory. From there you can run the server by:

node ./dist/server/main.js

This will spin up an express server which will listen to port 4000. If its in your local machine point your browser to http://localhost:4000/ to view the site. For servers you may need Node.js runners and proxy routes to point your domain to the application. 

If you have any comments or questions, please write in comments section below. I am happy to help.

SPAs or Single page applications are the new normal for Web industry. Now a days you often hear jargons like PWAs (Progressive Web Apps), Lazy Loading and SSR (Server Side Rendering). The main advantage of SPAs are they don’t reload the page but changes contents by manipulating the DOM elements. This comes with a caveat, the search engine bots and link scrapers have limited ability to render javascript DOM manipulations, thus SEO and Link Scraping becomes a challenge for SPAs. Angular have a Server Side Rendering package called Angular Universal (ngUniversal) which can give SSR capabilities in your Angular project.

In earlier days, to achieve server-side rendering in Angular, you need to wire up the NgUniversal package manually to your existing Angular project. And a separate build routine need to be created to build your SSR ready app.

With Angular 9 you can make your existing Angular project to have SSR in few easy steps.

Let’s create a new project in Angular with Angular CLI. To check if you have Angular CLI installed, you may run the following command.

ng --version

If you don’t have Angular CLI installed globally you may install it with your node package manager npm or yarn.

// Using NPM 

npm install -g @angular/cli

// Or using Yarn Package Manager

yarn global add @angular/cli

Once you have Angular CLI ready in your system, you can create a new Angular project by running :

ng new my-angular-ssr && cd my-angular-ssr

When you run the command above in your projects directory, a directory named my-angular-ssr will be created and a basic angular app will be crated with all node_modules installed.

Now spin up the development server with command:

ng serve --open

A default Angular landing page will be opened. When you do a view source in your browser, you will see only a place holder app-root tag and no content will be visible. You need the content to be rendered on the server as well, so that search engine bots and link scrapers can get the contents. 

Kill the development server with Ctrl+C. To get the Server Side Rendering enabled you need to run the following command: 

ng add @nguniversal/express-engine

That’s all to enable SSR in your Angular project. Now you can run SSR enabled development server with :

// Using NPM 

npm run dev:ssr —open

// Or using Yarn Package Manager

yarn dev:ssr —open

Now when the page opens, do a view source and you will see all the contents are there within the app-root tag. Congratulations you have your SSR enabled Angular project ready.

To build the Angular project with SSR you need to use the command :

// Using NPM 

npm run build:ssr

// Or using Yarn Package Manager

yarn build:ssr

dist directory in your Angular project directory will have a directory with the name of your project, in our case it is my-angular-ssr. That directory have two sub directories one browser and other server. The server directory have main.js file which is responsible for spinning up the express based server.

When you deploy your project, take the dist directory to the application root directory. From there you can run the server by:

node ./dist/server/main.js

This will spin up an express server which will listen to port 4000. If its in your local machine point your browser to http://localhost:4000/ to view the site. For servers you may need Node.js runners and proxy routes to point your domain to the application. 

If you have any comments or questions, please write in comments section below. I am happy to help.

Related Tags :

> @GKWrites > Angular 9 Server Side Rendering with Angular CLI