DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Implementing Micro Frontends in Angular: Step-By-Step Guide
  • Micro Frontends Architecture
  • Difference Between Bootstrap and AngularJS in 2022
  • Login With Google in Angular Firebase Application

Trending

  • Java Virtual Threads and Scaling
  • Performance Optimization Techniques for Snowflake on AWS
  • How to Format Articles for DZone
  • Unlocking the Benefits of a Private API in AWS API Gateway
  1. DZone
  2. Coding
  3. Frameworks
  4. Server-Side Rendering (SSR) Made Easy With Angular Universal 9+

Server-Side Rendering (SSR) Made Easy With Angular Universal 9+

By 
Rohana Liyanarachchi user avatar
Rohana Liyanarachchi
·
Aug. 25, 20 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
40.5K Views

Join the DZone community and get the full member experience.

Join For Free

The Angular team recently, announced a pre-render builder in Angular Universal, in Jan 2020 to be specific. Angular Universal is for server-side rending (SSR); with these new Angular Schematics available in Angular 9+, creating new SSR with Angular Universal or configuring your existing application to SSR is easier than ever. All the configuration can be done with Angular CLI.

What Is Server-Side Rendering (SSR)

The server returns a static web page fully complied with dynamic data ready to display on the browser. The fetching of the dynamic data is done by server-side scripts written by server-side languages. This is how we used to render web pages in the old days (PHP/ Perl/CGI), and it has recently gained traction with technologies, such as React and Express. It is SEO friendly and great for devices with low power. 

Server-side and client-side rendering

The server returns a complete static web page with all the elements required to display the browser and the client-side scripts required to make the page dynamic. 

What Is Client-Side Rendering (CSR)

The server returns a partial web page with no dynamic data, but it provides the client-side scripts necessary to fetch the data on demand asynchronously.

Server-side vs client-side scripts

The client is responsible for fetching the data upon loading a new page or based on user interaction. There are many Async calls to the server. CSR is not SEO friendly, as the content is always dynamic. 

Now, we are going to create a standard Angular app, which, by default, has been programmed for Client-side rendering. Then, using the new Angular Schematic, we are going to configure the application as server-side rendering (SSR).

Create a Standard Angular App

Step 1

Check you have the latest angular CLI (9 or greater). 

ng --version

if your CLI version is less than 9, then upgrade it to the latest version.

npm i -g @angular/cli

Step 2

Create a new Angular application.

 ng new angular-SSR-Demo

 The angular CLI will create a new angular project and install all packages. 

Step 3

Run the application and observe the content of the web page.

Once all the packages have installed successfully, we can run the application

 cd angular-SSR-Demo 

npm start

You will see the message that the development server is running at http://localhost:4200/.

Notice that the source of the HTML page served by the application. You don't see the static HTML for the content you see on the page, as most of the content has been loaded dynamically by client-side scripts. 

HTML preview


As you can see, the application created with CLI is configured to have Client-side rendering. With Angular Universal, we can configure the application as server-side rendering (SSR) easily. 

Configure for Server-Side rendering (SSR)

Step 4: Create New Files and Update the Existing Files

We are going to the latest Angular Universal schematic from @nguniversal. It is also going to add the express server to the project.

ng add @nguniversal/express-engine@next

This is what you will see:

Installing express-engine@next module

All required files have been created.  Notice following has been added to the package.json.  

JSON
 




x


 
1
"@angular/platform-server": "~10.0.9",
2
"@nguniversal/express-engine": "^10.0.0-rc.1",
3
"express": "^4.15.2",



Also, notice the newly added scripts/shortcuts in package.json

JSON
 




xxxxxxxxxx
1


 
1
"dev:ssr": "ng run angular-SSR-Demo:serve-ssr",
2
"serve:ssr": "node dist/angular-SSR-Demo/server/main.js",
3
"build:ssr": "ng build --prod && ng run angular-SSR-Demo:server:production",
4
"prerender": "ng run angular-SSR-Demo:prerender"



Note: Please correct the line export function app():void to export function app():any in the server.ts file if applicable. 

Step 5

Run the application and check the content of the web page.

npm run dev:ssr


The server has delivered a completely static HTML page with all elements in pure SSR. The client-side script is only needed for user interaction and is not responsible for fetching the data. However, in Angular Universal, there are hybrid approaches with universal templates, etc.  

This is just an introduction as there are many more. To find out more about the universal template engine, the Pre-Render or Ahead-of-Time (AOT) compiler, or how the routing works in SSR please check out: https://angular.io/guide/universal

Source codes are available here. 

Thank you for taking the time to read this article; your feedback is greatly appreciated.  

AngularJS application

Opinions expressed by DZone contributors are their own.

Related

  • Implementing Micro Frontends in Angular: Step-By-Step Guide
  • Micro Frontends Architecture
  • Difference Between Bootstrap and AngularJS in 2022
  • Login With Google in Angular Firebase Application

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!