Posted on February 18, 2023
Angular Universal is a server-side rendering solution that allows Angular applications to be rendered on the server-side before being sent to the client. This approach is beneficial for search engine optimization (SEO) because it allows search engines to crawl and index your website more easily. To optimize your website for SEO with Angular Universal, you can use the Title and Meta services provided by Angular. These services allow you to set the title and meta tags for each page of your application, which is important for search engine optimization. Setting up Angular Universal in an Angular application involves several steps Notice, the application will default start with http://localhost:4200/ The Title service allows you to set the title of each page dynamically, based on the content of the page. You can use the In this example, we import the Title service from the @angular/platform-browser module and inject it into our component. Then, in the constructor, we use the The Meta service allows you to set the meta tags for each page, which are important for providing additional information to search engines. You can use the addTag() method to add meta tags to each page. For example, you could add a meta description tag like this: In this example, we import the Meta service from the You will notice Server-side rendering with Angular Universal is important for public-facing websites for several reasons. Here is Official Guide. https://angular.io/guide/universal The command creates the following folder structure.
You will notice So what is happing inside now? In that object context, Angular initializes the application. The app makes requests to the backend, performs various asynchronous tasks, and applies any change detection from components to the DOM while still running inside the node.js/express environment. The render engine then serializes DOM into a string and serves up the string to the server. The server sends this HTML as a response to the GET request. Angular application on the server is destroyed after rendering. Note: Note: if you will run this code now this will work fine because it's just a dump component Follow another article about on - Part3 - Angular universal deploymentAngular universal for SEO. How do the Title and Meta services work?
1. Create a fresh angular project to start
ng new angular-universal-with-angular15
2. Start the application
npm start
3. Time to write a few lines of code for testing purposes
setTitle()
method to set the title for each page. For example, you could set the title of your homepage like this:import { Component } from '@angular/core';
import { Title } from '@angular/platform-browser';
@Component({
selector: 'app-homepage',
template: '<h1>Welcome to my website!</h1>',
})
export class HomepageComponent {
constructor(private titleService: Title) {
this.titleService.setTitle('My Website - Home');
}
}
setTitle()
method to set the title of the page.import { Component } from '@angular/core';
import { Meta } from '@angular/platform-browser';
@Component({
selector: 'app-homepage',
template: '<h1>Welcome to my website!</h1>',
})
export class HomepageComponent {
constructor(private metaService: Meta) {
this.metaService.addTag({ name: 'description', content: 'Welcome to my website!'});
}
}
@angular/platform-browser
module and inject it into our component. Then, in the constructor, we use the addTag()
method to add a meta description tag to the page.
What is the problem here in the view source (above image)?
<app-root></app-root>
but not showing all the child elements and dynamic meta tags, because the angular app is by default CSR (Client Side Rending)Why do we need to worry about view source for public-facing websites?
4. To Support SSR (Server Side Rendering) - Install a package called angular universal
ng add @nguniversal/express-engine
5. To start rendering your application with Universal on your local system, use the following command.
npm run dev:ssr
npm run dev:ssr
and navigate to http://localhost:4200/Here are the differences in view-source now
<app-root>............</app-root>
now showing all the child elements and dynamic meta tags because the angular app is now supporting SSR (Server Side Rending)Let's look at the
dist
folder
dist
folder when you run npm run dev:ssr
, you have sever
- contains the code that runs on the server-side when the application is being rendered, and browser
- contains the code that runs on the client-side when the application is being rendered.