Server Side Rendering In Angular
In this article, we learn how to use Angular Universal to implement server-side rendering. Before transmitting the pages to the browser, a method called server-side rendering, or SSR, renders them on the server. We will discover what Angular Universal is and why server-side rendering is essential. Later, we'll demonstrate how to build a sample server-side rendered Angular universal app. We also go over some very crucial information that you should understand before beginning to use Angular Universal for server-side rendering.
What is Angular Universal
Before transmitting the requested URL to the user's browser, Angular Universal renders the HTML view on the server. As a result, the app will load more quickly and, more crucially, be more search engine friendly.
Why is Server Side rendering required?
The Angular applications are one-page applications. There is a lot of javascript code in them. These Javascript scripts execute in the client's browser and instantly produce an HTML view. When a user switches between pages in the app, the app doesn't make any requests to the server. The page is rendered entirely by the browser. Only data and static resources, such as photos, are requested by the app from the backend. As a result, the user can use the application relatively quickly.
However, users are drawn to the website by search engines. Each page receives a rank based on a variety of criteria. The two main ranking factors are speed and content. SPA frameworks like Angular suffer from this.
Speed
The Angular apps load slowly at first for the following reasons:
- It has a lot of JavaScript files to download.
- Render the HTML to the view while running the JavaScript.
Content
The search engines must run JavaScript in order to display the material. Google does a decent job at interpreting the content and running the JavaScript, but you can't always rely on it to be accurate. Other search engines, such as Bing and others, are likewise unable to process it. The content won't be visible to social media crawlers like Facebook and Twitter because they don't run JavaScript.
By producing the content on the server and sending the user static HTML, we can fix the aforementioned issues.
What is Server-Side Rendering
The process is known as "Server-Side Rendering," or SSR involves running JavaScript on the server to create an HTML file that is then sent to the client.
For instance, the server finds index.html when it receives a request for a specific page. The server now delivers it to a renderer process rather than providing it to the browser. The Renderer creates HTML while running the Angular Framework (much like it would in a browser). If necessary to obtain the data, it also sends HTTP requests to the backend server. The server then sends the browser the HTML it produced.
The user can now see the HTML thanks to the browser. The browser has not yet downloaded Angular, thus the page will not be affected by mouse or keyboard events at this time. The page isn't interactive yet.
The Angular framework is starting to download in the browser. After Angular bootstraps and loads, it begins to record user events. It becomes interactive on the page. From this point on, Angular takes over and all subsequent client-side navigation occurs.
Server Side rendering Vs Client-Side Rendering
The contrast between client-side rendering and server-side rendering is shown in the following table.
Sr.No | Client-Side Rendering | Server Side Rendering |
---|---|---|
1 | The user sends the requests for a page | The user sends the requests for a page |
2 | The server locates the index.html and sends it to the user | The server locates the index.html and sends it to the Renderer Process running in Server |
3 | Renderer prepares the initial page, by executing the Angular Framework and sends it to the browser | |
4 | The Browser displays the blank page, but there is no content there | The browser shows the initial page to the user. The page may be non-interactive at this time. |
5 | The browser downloads the CSS & JavaScript | The browser downloads the CSS & Javascript |
6 | The browser executes Angular Framework (JavaScript) & Renders the page | The browser executes Angular Framework (JavaScript) |
7 | The user sees the Interactive Page | The page becomes interactive. |
Content and SSR
There is no need for search engines to run the javascript because the server creates the content. The HTML is visible to social media crawlers as well.
Speed and SSR
Does it address the speed issue? The quick response is "no"
SSR implementation alone won't make a site faster. mainly because all you are doing is shifting the rendering from the browser to the server. In reality, a busy server may cause your site to load slowly.
However, it will enhance the initial FCP or paint.
However, when you cache the produced response, you experience excellent speed. By serving the user a cached answer, the rendering step is cut off, and the user receives the HTML immediately. Only the SSR allows for this.
Example
Using the Angular CLI, create a new Angular project.
ng new ssr
View the page source by doing a right-click anywhere on the browser windows. You'll notice there isn't any material. When they explore your website, search engines will detect this.
<html lang="en"> <head> <meta charset="utf-8"></meta> <title>Ssr</title> <base href="/"></base> <meta content="width=device-width, initial-scale=1" name="viewport"></meta> <link href="favicon.ico" rel="icon" type="image/x-icon"></link> </head> <body> <app-root></app-root> <script src="runtime.js" type="module"></script><script src="polyfills.js" type="module"></script><script src="styles.js" type="module"></script><script src="vendor.js" type="module"></script><script src="main.js" type="module"></script></body> </html>
Adding Angular Universal
ng add @nguniversal/express-engine --clientProject ssr
The angular.json file under the projects node contains the project name.
{ "$schema": "./node_modules/@angular/cli/lib/config/schema.json", "version": 1, "newProjectRoot": "projects", "projects": { "ssr": { ====> Name of the project "projectType": "application", "schematics": {},