Welcome to our deep dive into Angular Universal, a powerful feature that enables Server-Side Rendering (SSR) in Angular applications. Let's embark on a journey to make our Angular apps more efficient, SEO-friendly, and user-friendly!
Server-Side Rendering is a technique to render the application on the server rather than the client. It has numerous benefits such as:
Angular Universal, a part of the Angular ecosystem, allows us to render Angular applications on the server-side, making it possible to leverage the benefits of Server-Side Rendering.
To get started with Angular Universal, first, we need to install the necessary packages:
npm install --save @angular/platform-server @nguniversal/module-map-ngfactory-loader @nguniversal/express-engineNext, we need to configure our Angular CLI workspace:
ng generate universal:serverThis command will create a server entry point and configure the necessary settings.
Let's create a simple component that we can render on the server:
ng generate component universal-exampleModify the universal-example.component.ts as follows:
import { Component, OnInit } from '@angular/core';
import { ServerRenderService } from '../services/server-render.service';
@Component({
selector: 'app-universal-example',
templateUrl: './universal-example.component.html',
styleUrls: ['./universal-example.component.css']
})
export class UniversalExampleComponent implements OnInit {
constructor(private serverRenderService: ServerRenderService) {}
ngOnInit() {
this.serverRenderService.render();
}
}Here, we've injected the ServerRenderService and called the render() method in the ngOnInit lifecycle hook.
Create a new service named server-render.service.ts:
import { Injectable } from '@angular/core';
import { ServerResponse } from 'http';
@Injectable({
providedIn: 'root'
})
export class ServerRenderService {
constructor() {}
render() {
// Render the component on the server-side
this.serverRender();
}
private serverRender() {
// Implementation of server-side rendering logic
// (This example does not cover the actual implementation)
}
}We'll implement the serverRender() method shortly.
Now, we need to configure our server to use Angular Universal:
main.server.ts with the following:import { enableProdMode } from '@angular/core';
import { platformServer } from '@angular/platform-server';
import { AppServerModuleNgFactory, LAZY_MODULE_MAP } from './dist/server/main';
enableProdMode();
export function main(argv: string[]): Promise<void> {
return platformServer(AppServerModuleNgFactory, LAZY_MODULE_MAP).bootstrapApplication(argv);
}src/app/app.server.module.ts:import { NgModule } from '@angular/core';
import { ServerModule } from '@angular/platform-server';
import { AppServerModule } from './app.server.module';
@NgModule({
imports: [
AppServerModule,
ServerModule,
]
})
export class AppServerModule { }We'll now implement the serverRender() method in our server-render.service.ts:
import { Response, Request } from 'express';
import { renderModuleFactory } from '@nguniversal/express-engine';
// ...
private async serverRender() {
const app = express();
app.engine('.html', ngExpressEngine({
bootstrap: AppModuleNgFactory,
providers: [ServerRenderService],
}));
app.get('*', async (req: Request, res: Response) => {
const { AppComponent } = await renderModuleFactory(req, res);
// We could further process the AppComponent here
res.send(AppComponent.render());
});
app.listen(4000, () => {
console.log('Server-side rendering running on port 4000');
});
}With this implementation, our server will now render Angular components on the server-side and send the HTML to the client.
To test our Universal component, navigate to http://localhost:4000 in your browser.
We've successfully set up and implemented Server-Side Rendering with Angular Universal in our Angular application. This setup can be further extended and optimized to suit real-world projects.
What is the main advantage of Server-Side Rendering in Angular applications?