Reader small image

You're reading from  Angular Projects - Third Edition

Product typeBook
Published inJul 2023
Reading LevelIntermediate
PublisherPackt
ISBN-139781803239118
Edition3rd Edition
Languages
Tools
Right arrow
Author (1)
Aristeidis Bampakos
Aristeidis Bampakos
author image
Aristeidis Bampakos

Aristeidis Bampakos is a Web Development Team Lead at Plex-Earth who specializes in the development of web applications with Angular. He has been an Angular Google Developer Expert (GDE) since 2020 and works as an Angular Senior Tech Instructor at Code.Hub, a private educational institute, where he nurtures aspiring Angular developers and professionals. He is also the author of Angular Projects with Packt.
Read more about Aristeidis Bampakos

Right arrow

Integrating Angular Universal

Angular Universal is an Angular library that enables an Angular CLI application to be rendered on the server. An SSR application increases the loading speed of an Angular application and improves the loading of the first page.To install Angular Universal in an existing Angular CLI application, we will use the following command of the Angular CLI:

ng add @nguniversal/express-engine

The previous command uses the ng add command of the Angular CLI to install the @nguniversal/express-engine npm package. The @nguniversal/express-engine package is the heart of the Angular Universal library and consists of a Node.js Express web server at its core.When we execute the preceding command to install Angular Universal, we are not only installing the library but also modifying our Angular CLI workspace with the following files:

  • angular.json: This creates new entries in the architect section to build and enable our Angular Universal application. One of these entries is...

Prerendering content during build

The package.json file of our Angular CLI workspace contains the prerender npm script that we can use to improve the first loading of our application. The script runs the prerender command from the architect section of the angular.json configuration file and prerenders the content of our application during build time. Let's see the effect that prerendering will have on our GitHub portfolio application:

  1. Execute the following npm command to generate a prerendered version of the application:
npm run prerender
  1. The preceding command will output a production bundle of the application into the dist\gh-portfolio\browser folder.
  2. Navigate to the dist\gh-portfolio\browser folder and you should see two HTML files, the index.html and the index.original.html file.
  3. Open the index.original.html file and locate the <app-root> HTML element. This is the main component of our Angular application, where Angular will render the content of our application in...

Enhancing SEO capabilities

SEO optimizes a website to be correctly indexed by a web crawler. A web crawler is a special-purpose software on most search engines and can identify and index websites so that they are easily discoverable and linkable through their platforms.Angular Universal does a great job of SEO by prerendering content during build time. Some web crawlers cannot execute JavaScript and build the dynamic content of an Angular application. Prerendering with Angular Universal eliminates the need for JavaScript, thus allowing web crawlers to do their best to identify the web application.We can also help SEO by defining several tags in the <head> element of the main index.html file of an Angular application, such as title, viewport, and charset:

<head>
  <meta charset="utf-8">
  <title>GhPortfolio</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
...

Replaying events with preboot

In the Integrating Angular Universal section, we saw that an SSR application does not respond to user events other than navigations with the routerLink directive until it is entirely bootstrapped. We will now learn how to queue and replay those events when the application has been fully loaded using the preboot library. Let's see how to use it:

  1. Execute the following npm command to install the library:
npm install preboot
  1. Open the app.module.ts file and import PrebootModule from the preboot npm package:
import { PrebootModule } from 'preboot';
  1. Add the PrebootModule class to the imports array of the @NgModule decorator:
@NgModule({
  declarations: [
    AppComponent,
    PersonalInfoComponent,
    PanelComponent,
    RepositoriesComponent,
    OrganizationsComponent
  ],
  imports: [
    BrowserModule.withServerTransition({ appId:
     'serverApp' }),
    HttpClientModule,
    TransferHttpCacheModule,
    PrebootModule.withConfig...

Summary

In this project, we build a portfolio application for our GitHub profile. Initially, we learned how to interact with the GitHub API in a new Angular application. We also used Bootstrap CSS and Bootstrap Icons to provide a beautiful user interface for our portfolio application.We then saw how to convert our Angular application into an SSR application using Angular Universal. We learned how to benefit from prerendering content when users have low-end and slow-performant devices and some of the potential pitfalls of this technique.We used some of the available SEO techniques that the Angular framework offers to improve the discoverability of our application. Finally, we installed the preboot library to enhance the user experience of our application during loading.In the next chapter, we will learn about the monorepo architecture and how we can manage the state of an Angular application.

Practice questions

Let's take a look at a few practice questions:

  1. How do we subscribe to an observable in the template of a component?
  2. Which command do we use for installing Angular Universal?
  3. How can we differentiate programmatically between browser and server platforms?
  4. Which command generates a prerendered version of an SSR application?
  5. Which Angular module do we use to transfer the state from the server to the browser?
  6. Which Angular service do we use to set the title of an Angular application?
  7. Which Angular service do we use to set meta tags in an Angular application?
  8. What is the purpose of the preboot library?
  9. How do we enable preboot in an SSR application?

Further reading

Here are some links to build upon what we learned in the chapter:

Enhancing SEO capabilities

SEO optimizes a website to be correctly indexed by a web crawler. A web crawler is special-purpose software on most search engines and can identify and index websites so that they are easily discoverable and linkable through their platforms.

Angular Universal does a great job of SEO by prerendering content during build time. Some web crawlers cannot execute JavaScript and build the dynamic content of an Angular application. Prerendering with Angular Universal eliminates the need for JavaScript, thus allowing web crawlers to do their best to identify the web application.

We can also help SEO by defining several tags in the <head> element of the main index.html file of an Angular application, such as title, viewport, and charset:

<head>
  <meta charset="utf-8">
  <title>GhPortfolio</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale...

Summary

In this project, we built a portfolio application for our GitHub profile. Initially, we learned how to interact with the GitHub API in a new Angular application. We also used Bootstrap CSS and Bootstrap Icons to provide a beautiful user interface for our portfolio application.

We then saw how to convert our Angular application into an SSR application using Angular Universal. We learned how to benefit from prerendering content when users have low-end and slow-performing devices and some of the potential pitfalls of this technique.

We used some of the available SEO techniques that the Angular framework offers to improve the discoverability of our application.

In the next chapter, we will learn about the monorepo architecture and how we can manage the state of an Angular application.

Practice questions

Let’s take a look at a few practice questions:

  1. How do we subscribe to an observable in the template of a component?
  2. What command do we use to install Angular Universal?
  3. How can we differentiate programmatically between browser and server platforms?
  4. What command generates a prerendered version of an SSR application?
  5. What Angular module do we use to transfer the state from the server to the browser?
  6. What Angular service do we use to set the title of an Angular application?
  7. What Angular service do we use to set meta tags in an Angular application?

Further reading

Here are some links to build upon what we learned in the chapter:

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Angular Projects - Third Edition
Published in: Jul 2023Publisher: PacktISBN-13: 9781803239118
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Aristeidis Bampakos

Aristeidis Bampakos is a Web Development Team Lead at Plex-Earth who specializes in the development of web applications with Angular. He has been an Angular Google Developer Expert (GDE) since 2020 and works as an Angular Senior Tech Instructor at Code.Hub, a private educational institute, where he nurtures aspiring Angular developers and professionals. He is also the author of Angular Projects with Packt.
Read more about Aristeidis Bampakos