docs/en/framework/ui/angular/ssr-configuration.md
//[doc-seo]
{
"Description": "Learn how to configure Server-Side Rendering (SSR) for your Angular application in the ABP Framework to improve performance and SEO."
}
Server-Side Rendering (SSR) is a process that involves rendering pages on the server, resulting in initial HTML content that contains the page state. This allows the browser to show the page to the user immediately, before the JavaScript bundles are downloaded and executed.
SSR improves the performance (First Contentful Paint) and SEO (Search Engine Optimization) of your application.
The ABP Framework provides a schematic to easily add SSR support to your Angular application.
Run the following command in the root folder of your Angular application:
yarn ng generate @abp/ng.schematics:ssr-add
Alternatively, you can specify the project name if you have a multi-project workspace:
yarn ng generate @abp/ng.schematics:ssr-add --project MyProjectName
This command automates the setup process by installing necessary dependencies, creating server-side entry points, and updating your configuration files.
When you run the schematic, it performs the following actions:
It adds the following packages to your package.json:
{
"dependencies": {
"express": "^4.18.2",
"openid-client": "^5.6.4"
},
"devDependencies": {
"@types/express": "^4.17.17"
}
}
For Webpack projects only:
The changes depend on the builder used in your project (Application Builder or Webpack).
If your project uses the Application Builder (@angular/build:application), the schematic:
serve:ssr:project-name to serve the SSR application.build target to enable SSR (outputMode: 'server') and sets the SSR entry point.{
"projects": {
"MyProjectName": {
"architect": {
"build": {
"options": {
"outputPath": "dist/MyProjectName",
"outputMode": "server",
"ssr": {
"entry": "src/server.ts"
}
}
}
}
}
}
}
tsconfig to include server.ts.If your project uses the Webpack Builder (@angular-devkit/build-angular:browser), the schematic:
dev:ssr, serve:ssr, build:ssr, and prerender scripts.server, serve-ssr, and prerender.tsconfig to include server.ts.bootstrapApplication.platformBrowserDynamic.import {
AngularNodeAppEngine,
createNodeRequestHandler,
isMainModule,
writeResponseToNodeResponse,
} from '@angular/ssr/node';
import express from 'express';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { environment } from './environments/environment';
import { ServerCookieParser } from '@abp/ng.core';
import * as oidc from 'openid-client';
// ... (OIDC configuration and setup)
const app = express();
const angularApp = new AngularNodeAppEngine();
// ... (OIDC routes: /authorize, /logout, /)
/**
* Serve static files from /browser
*/
app.use(
express.static(browserDistFolder, {
maxAge: '1y',
index: false,
redirect: false,
}),
);
/**
* Handle all other requests by rendering the Angular application.
*/
app.use((req, res, next) => {
angularApp
.handle(req)
.then(response => {
if (response) {
res.cookie('ssr-init', 'true', {...secureCookie, httpOnly: false});
return writeResponseToNodeResponse(response, res);
} else {
return next()
}
})
.catch(next);
});
// ... (Start server logic)
export const reqHandler = createNodeRequestHandler(app);
import { RenderMode, ServerRoute } from '@angular/ssr';
export const serverRoutes: ServerRoute[] = [
{
path: '**',
renderMode: RenderMode.Server
}
];
import { mergeApplicationConfig, ApplicationConfig, provideAppInitializer, inject, PLATFORM_ID, TransferState } from '@angular/core';
import { isPlatformServer } from '@angular/common';
import { provideServerRendering, withRoutes } from '@angular/ssr';
import { appConfig } from './app.config';
import { serverRoutes } from './app.routes.server';
import { SSR_FLAG } from '@abp/ng.core';
const serverConfig: ApplicationConfig = {
providers: [
provideAppInitializer(() => {
const platformId = inject(PLATFORM_ID);
const transferState = inject<TransferState>(TransferState);
if (isPlatformServer(platformId)) {
transferState.set(SSR_FLAG, true);
}
}),
provideServerRendering(withRoutes(serverRoutes)),
],
};
export const config = mergeApplicationConfig(appConfig, serverConfig);
<div id="lp-page-loader"></div>) to prevent hydration mismatches.After the installation is complete, you can run your application with SSR support.
To serve the application with SSR in development:
yarn start
# or
yarn ng serve
To serve the built application (production):
yarn run serve:ssr:project-name
Development:
yarn run dev:ssr
Production:
yarn run build:ssr
yarn run serve:ssr
The schematic installs openid-client to handle authentication on the server side. This ensures that when a user accesses a protected route, the server can validate their session or redirect them to the login page before rendering the content.
Ensure your OpenID Connect configuration (in
environment.tsorapp.config.ts) is compatible with the server environment.
Angular 21 provides different rendering modes that you can configure per route in the app.routes.server.ts file to optimize performance and SEO.
import { RenderMode, ServerRoute } from '@angular/ssr';
export const serverRoutes: ServerRoute[] = [
// Server-Side Rendering - renders on every request
{
path: 'dashboard',
renderMode: RenderMode.Server
},
// Prerender (SSG) - renders at build time
{
path: 'about',
renderMode: RenderMode.Prerender
},
// Client-Side Rendering - renders only in browser
{
path: 'admin/**',
renderMode: RenderMode.Client
},
// Default fallback
{
path: '**',
renderMode: RenderMode.Server
}
];
Renders HTML on every request. Best for dynamic content, personalized pages, and pages requiring authentication.
Generates static HTML at build time. Best for marketing pages, blog posts, and content that doesn't change frequently.
For dynamic routes, use getPrerenderParams:
{
path: 'blog/:slug',
renderMode: RenderMode.Prerender,
getPrerenderParams: async () => {
const posts = await fetchBlogPosts();
return posts.map(post => ({ slug: post.slug }));
}
}
Traditional client-side rendering. Best for highly interactive applications and admin panels that don't need SEO.
Combine different modes in one application for optimal results:
export const serverRoutes: ServerRoute[] = [
// Static pages
{ path: '', renderMode: RenderMode.Prerender },
{ path: 'about', renderMode: RenderMode.Prerender },
// Dynamic pages
{ path: 'account', renderMode: RenderMode.Server },
{ path: 'orders', renderMode: RenderMode.Server },
// Admin area
{ path: 'admin/**', renderMode: RenderMode.Client },
];
Hydration is the process where Angular attaches to server-rendered HTML and makes it interactive. The ABP schematic automatically configures hydration for your application.
Problem: Browser APIs on Server
// ❌ Bad - will fail on server
const width = window.innerWidth;
// ✅ Good - check platform
import { isPlatformBrowser } from '@angular/common';
import { PLATFORM_ID, inject } from '@angular/core';
export class MyComponent {
platformId = inject(PLATFORM_ID);
getWidth() {
if (isPlatformBrowser(this.platformId)) {
return window.innerWidth;
}
return 0;
}
}
Problem: Random or Time-Based Values
// ❌ Bad - generates different values on server and client
id = Math.random();
currentTime = new Date();
// ✅ Good - use TransferState for consistent data
import { TransferState, makeStateKey } from '@angular/core';
TIME_KEY = makeStateKey<string>('time');
transferState = inject<TransferState>(TransferState);
time: string;
constructor() {
if (isPlatformServer(this.platformId)) {
this.time = new Date().toISOString();
this.transferState.set(this.TIME_KEY, this.time);
} else {
const timeFromCache = this.transferState.get(this.TIME_KEY, new Date().toISOString());
this.time = timeFromCache;
}
}
Enable Debug Tracing:
// app.config.ts
import { provideClientHydration, withDebugTracing } from '@angular/platform-browser';
export const appConfig: ApplicationConfig = {
providers: [
provideClientHydration(withDebugTracing()),
]
};
Configure your SSR application using environment variables in server.ts:
// server.ts
const PORT = process.env['PORT'] || 4000;
const HOST = process.env['HOST'] || 'localhost';
// Start the server
if (isMainModule(import.meta.url)) {
app.listen(PORT, () => {
console.log(`Server running on http://${HOST}:${PORT}`);
});
}
For production, set environment variables:
# .env file or environment configuration
NODE_ENV=production
PORT=4000
HOST=0.0.0.0
API_URL=https://api.yourdomain.com
To deploy your Angular SSR application to a production server:
yarn build
# or if using Webpack builder
yarn run build:ssr
Copy the dist/MyProjectName folder to your server:
dist/MyProjectName/
├── browser/ # Client-side bundles
└── server/ # Server-side bundles (server.mjs)
On your server, install only the required dependencies (schematic already added them to package.json):
npm install --production
Required dependencies:
express: Web server frameworkopenid-client: Authentication supportDevelopment/Testing:
node server/server.mjs
Production (with PM2):
Use PM2 to keep your application alive and manage restarts:
npm install -g pm2
pm2 start server/server.mjs --name "my-app"
pm2 startup # Configure PM2 to start on boot
pm2 save # Save current process list
Browser APIs don't exist on the server. Always check the platform:
import { isPlatformBrowser } from '@angular/common';
if (isPlatformBrowser(this.platformId)) {
// Safe to use window, document, localStorage, etc.
}
ABP Core provides AbpLocalStorageService that implements the Storage interface and works safely on both server and client:
import { AbpLocalStorageService } from '@abp/ng.core';
@Injectable({ providedIn: 'root' })
export class MyService {
private storage = inject(AbpLocalStorageService);
saveData(key: string, value: string): void {
// Safe on both server and client
this.storage.setItem(key, value);
}
getData(key: string): string | null {
// Returns null on server, actual value on client
return this.storage.getItem(key);
}
}
AbpLocalStorageService implements all Storage methods:
getItem(key: string): string | nullsetItem(key: string, value: string): voidremoveItem(key: string): voidclear(): voidkey(index: number): string | nulllength: numberIf you see "NG0500" errors in the console:
TransferState for data consistencyABP Core provides a transferStateInterceptor that automatically prevents duplicate HTTP GET requests during hydration. When you use provideAbpCore(), this interceptor is already active.
How it works:
TransferState// app.config.ts
import { provideAbpCore } from '@abp/ng.core';
export const appConfig: ApplicationConfig = {
providers: [
provideAbpCore(),
// transferStateInterceptor is automatically included
]
};
The interceptor works with all HTTP GET requests made through HttpClient:
// This service automatically benefits from the interceptor
@Injectable({ providedIn: 'root' })
export class UserService {
private http = inject(HttpClient);
getUsers() {
// On server: Response is cached in TransferState
// On client: Cached response is used (no duplicate request)
return this.http.get<User[]>('/api/users');
}
}
[!NOTE] The interceptor only works with GET requests. POST, PUT, DELETE, and PATCH requests are not cached.
The ABP Angular SSR schematic provides:
Configure render modes based on your needs, handle platform differences properly, and use environment variables for deployment configuration.