Skip to main content

Overview

The @angular/platform-server package provides the infrastructure for rendering Angular applications on the server. This enables server-side rendering (SSR) for improved performance, SEO, and social media sharing.

Rendering Functions

renderApplication

Bootstraps and renders a standalone Angular application to an HTML string. This is the recommended approach for SSR with standalone components.
(context: BootstrapContext) => Promise<ApplicationRef>
required
Function that bootstraps the application. Receives a BootstrapContext with the platform reference.
object
required
Configuration options for the render operation.
string | Document
The HTML document to render into, either as a string or Document instance. Defaults to a minimal HTML template.
string
The URL for the current render request. Used for routing and generating absolute URLs.
Provider[]
Additional platform-level providers for the render operation.
Returns: Promise<string> - The rendered HTML as a string. Example:
Express Integration:
renderApplication automatically waits for the application to stabilize before rendering. This ensures all async operations complete before generating the HTML.

renderModule

Bootstraps and renders an NgModule-based Angular application to an HTML string. Use this for applications using NgModules instead of standalone components.
Type<T>
required
The NgModule class to bootstrap.
object
required
Configuration options for the render operation.
string | Document
The HTML document to render into.
string
The URL for the current render request.
StaticProvider[]
Additional platform-level providers.
Returns: Promise<string> - The rendered HTML as a string. Example:

Platform

platformServer

Creates a server platform instance for Angular applications. This is used internally by renderApplication and renderModule but can be used directly for advanced scenarios.
StaticProvider[]
Additional platform-level providers.
Returns: PlatformRef - Server platform reference. Example:

ServerModule

NgModule that provides server-specific implementations for Angular’s browser APIs. Import this in your server-specific app module.
Example:
For standalone applications, use provideServerRendering() instead of ServerModule.

Providers

provideServerRendering

Provides server-side rendering configuration for standalone applications. Use this in your server application configuration.
Example:
Complete Server Setup:

Services

PlatformState

Service that provides access to the server-rendered document and rendering utilities.
Methods:
() => Document
Returns the server-side Document instance.
() => string
Renders the current document to an HTML string.
Example:

Configuration Tokens

INITIAL_CONFIG

Injection token for providing initial server configuration.
PlatformConfig Interface:
Example:

BEFORE_APP_SERIALIZED

Injection token for callbacks that run before the application is serialized to HTML. Useful for finalizing state or performing cleanup.
Example:
Async Callbacks:

Server-Side Rendering Setup

Basic SSR Setup

For a complete SSR setup with standalone components: 1. Install dependencies:
2. Create server configuration (app.config.server.ts):
3. Create server entry point (main.server.ts):
4. Create Express server:

Transfer State

Transfer state allows data to be shared between server and client, avoiding duplicate HTTP requests.
Using TransferState Service:
When using provideHttpClient() with withFetch(), transfer state is automatically handled for HTTP requests.

Best Practices

1. Handle Platform Differences

Use platform checks for code that should only run on server or browser:

2. Avoid Browser-Only APIs

Avoid direct references to browser globals like window, document, localStorage:

3. Optimize Performance

Use BEFORE_APP_SERIALIZED for cleanup and optimization:

4. Handle Absolute URLs

Ensure URLs are absolute when rendering on the server:

@angular/platform-browser

Browser platform APIs and client hydration

TransferState

Share data between server and client

HttpClient

HTTP requests with SSR support

Router

Routing in SSR applications