José Matos
•01 Apr 2023
Are you struggling to make your Angular application SEO-friendly? You are not alone! The initial release of Angular made it difficult for search engine crawlers to index client-side rendered pages. But, the release of Angular Universal has bridged the gap between client-side and server-side rendering, making it easy to create SEO-friendly applications.
In this article, we will discuss Angular Universal, how it works, and how you can use it to make your Angular application SEO-friendly.
Angular Universal is a server-side rendering (SSR) solution for Angular. It renders your application on the server-side before it gets to the client-side, hence making it SEO-friendly. With Angular Universal, your application gets indexed by search engines, crawlers, and other third-party tools.
Angular Universal works by rendering your Angular application on the server-side using Node.js. It runs your application just like a browser but on the server, creating static HTML pages that can be crawled and indexed by search engines.
Here is how Angular Universal works:
Implementing Angular Universal is easy. Here are the steps:
npm install --save @angular/platform-server
touch app.server.module.ts
import { NgModule } from '@angular/core';
import { ServerModule } from '@angular/platform-server';
import { AppModule } from './app.module';
import { AppComponent } from './app.component';
@NgModule({
imports: [
AppModule,
ServerModule
],
bootstrap: [AppComponent],
})
export class AppServerModule { }
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { AppServerModule } from './app.server.module';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule.withServerTransition({appId: 'my-app'}),
AppServerModule
],
bootstrap: [AppComponent]
})
export class AppModule { }
touch server.ts
import 'zone.js/dist/zone-node';
import { ngExpressEngine } from '@nguniversal/express-engine';
import * as express from 'express';
import { join } from 'path';
import { AppServerModule } from './src/app/app.server.module';
const app = express();
const PORT = process.env.PORT || 3000;
const DIST_FOLDER = join(process.cwd(), 'dist');
app.engine('html', ngExpressEngine({
bootstrap: AppServerModule
}));
app.set('view engine', 'html');
app.set('views', join(DIST_FOLDER, 'browser'));
app.get('*.*', express.static(join(DIST_FOLDER, 'browser')));
app.get('*', (req, res) => {
res.render('index', { req });
});
app.listen(PORT, () => {
console.log(`Node server listening on http://localhost:${PORT}`);
});
"build:ssr": "npm run build:client-and-server-bundles && npm run compile:server",
"serve:ssr": "node dist/server"
npm run build:ssr
npm run serve:ssr
1. SEO-friendly: With Angular Universal, your application becomes more SEO-friendly.
2. Improved performance: With server-side rendering, users receive content faster, which leads to improved performance.
3. Better accessibility: Server-side-rendered pages are more accessible as they can be read by assistive technologies.
4. Better social media sharing: Server-side rendered pages work well with social media sharing tools as they provide metadata for search engine crawlers.
In this article, we covered Angular Universal and how it can help make your Angular application SEO-friendly. We’ve also discussed how Angular Universal works and how to implement it in your application. Building SEO-friendly applications can be challenging, but with Angular Universal, you can solve this problem with ease.