Skip to main content

Overview

Server-Side Rendering (SSR) generates your Angular application’s HTML on the server, providing faster initial page loads, better SEO, and improved performance on low-powered devices.

Benefits of SSR

Better SEO

Search engines can crawl fully-rendered pages with content.

Faster First Paint

Users see content faster before JavaScript loads and executes.

Social Sharing

Preview images and metadata work correctly on social platforms.

Low-End Devices

Better performance on devices with limited processing power.

Setup

Adding SSR to Existing Application

Use Angular CLI to add SSR support:
This command:
  • Installs necessary dependencies
  • Creates server-side configuration files
  • Updates angular.json with server build configuration
  • Adds server entry point files

Manual Setup

For more control, set up SSR manually:

Server Configuration

Main Server Bootstrap

Server App Configuration

Rendering Applications

Using renderApplication

Using renderModule (NgModule)

Platform-Specific Code

Detecting Platform

Use platform checks to run code conditionally.

Conditional Browser APIs

HTTP and Data Transfer

TransferState

Avoid duplicate HTTP requests by transferring data from server to client.

HTTP Interceptor for TransferState

Pre-rendering (SSG)

Generate static pages at build time for even better performance.

Configuration

Dynamic Route Discovery

SEO Optimization

Meta Tags Service

Using SEO Service

Best Practices

Use TransferState

Avoid duplicate HTTP requests by transferring data from server to client.

Lazy Load Heavy Components

Defer loading of non-critical components to improve initial render time.

Handle Platform Differences

Always check platform before accessing browser-only APIs.

Optimize Images

Use responsive images and lazy loading for better performance.
Avoid using browser-specific APIs like window, document, localStorage without platform checks. These will cause errors during server-side rendering.

Additional Resources