José Matos
•31 Mar 2023
As an Angular developer, you may be familiar with the concept of server-side rendering, but have you ever considered using Angular Universal for this purpose? In this article, we’ll discuss what Angular Universal is and how it can benefit your project.
Angular Universal is an official library provided by the Angular team that enables server-side rendering (SSR) for Angular applications. SSR involves generating HTML on the server-side and sending it back to the client instead of relying on JavaScript to render the content in the browser. This can lead to better performance, SEO, and user experience.
One of the biggest benefits of using Angular Universal for SSR is improved performance and user experience. With SSR, the initial rendering of your application happens on the server-side, which means that the user gets the fully rendered page faster. This can help reduce the perceived load time of your application, leading to higher engagement and satisfaction.
Additionally, since the HTML is generated on the server, the content is immediately available to the user. This means that if the user has a slow internet connection or if they have disabled JavaScript, they will still be able to see the content of your website.
Server-side rendering also helps with search engine optimization (SEO). Search engines like Google have an easier time crawling and indexing content when it’s presented as static HTML instead of dynamic JavaScript. By using Angular Universal for SSR, you can improve your website’s search engine rankings and visibility.
Another benefit of using Angular Universal for SSR is reduced server load. With client-side rendering (CSR), each client must generate the HTML and JavaScript for the application, which can put a strain on your server. However, with SSR, the server does the heavy lifting and only needs to send static HTML to the client, which can lead to a lower server load and improved scalability.
Finally, server-side rendering can also help with accessibility. Since the HTML is generated on the server, it’s easier to ensure that it meets accessibility standards. This can help improve the experience for users who rely on screen readers or other assistive technologies.
Implementing Angular Universal in your project is relatively straightforward. First, you’ll need to install the necessary packages:
ng add @nguniversal/express-engine
This will install the Express engine for Angular Universal.
Next, you’ll need to update the AppModule to support server-side rendering. This involves importing the necessary platform-server and platform-browser packages and updating the providers:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ServerModule } from '@angular/platform-server';
import { AppModule } from './app.module';
import { AppComponent } from './app.component';
@NgModule({
imports: [BrowserModule.withServerTransition({appId: 'my-app'}), ServerModule, AppModule],
bootstrap: [AppComponent],
})
export class AppServerModule {}
You’ll also need to update your main.ts file to use the AppServerModule:
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppServerModule } from './app/app.server.module';
if (process.env.NODE_ENV === 'production') {
enableProdMode();
}
document.addEventListener('DOMContentLoaded', () => {
platformBrowserDynamic().bootstrapModule(AppServerModule);
});
Finally, you’ll need to update your server.ts file to use the Express engine:
import 'zone.js/dist/zone-node';
import * as express from 'express';
import { join } from 'path';
import { ngExpressEngine } from '@nguniversal/express-engine';
import { AppServerModule } from './src/main.server';
const app = express();
const PORT = process.env.PORT || 4000;
const DIST_FOLDER = join(process.cwd(), 'dist');
app.engine('html', ngExpressEngine({
bootstrap: AppServerModule
}));
app.set('view engine', 'html');
app.set('views', join(DIST_FOLDER, 'my-app'));
app.get('*.*', express.static(join(DIST_FOLDER, 'my-app')));
app.get('*', (req, res) => {
res.render('index', {req});
});
app.listen(PORT, () => {
console.log(`Node server listening on http://localhost:${PORT}`);
});
With these changes in place, you’re ready to build and run your application with server-side rendering!
Angular Universal is a powerful library that can help improve the performance, SEO, and user experience of your Angular application. By generating HTML on the server-side, you can reduce load times, improve SEO, and provide a better experience for users with slow connections or assistive technologies. By following the steps outlined in this article, you can easily implement Angular Universal in your project and start reaping the benefits of server-side rendering.