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.
Promise<string> - The rendered HTML as a string.
Example:
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.
Promise<string> - The rendered HTML as a string.
Example:
Platform
platformServer
Creates a server platform instance for Angular applications. This is used internally byrenderApplication and renderModule but can be used directly for advanced scenarios.
StaticProvider[]
Additional platform-level providers.
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.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.Services
PlatformState
Service that provides access to the server-rendered document and rendering utilities.() => Document
Returns the server-side Document instance.
() => string
Renders the current document to an HTML string.
Configuration Tokens
INITIAL_CONFIG
Injection token for providing initial server configuration.BEFORE_APP_SERIALIZED
Injection token for callbacks that run before the application is serialized to HTML. Useful for finalizing state or performing cleanup.Server-Side Rendering Setup
Basic SSR Setup
For a complete SSR setup with standalone components: 1. Install dependencies:Transfer State
Transfer state allows data to be shared between server and client, avoiding duplicate HTTP requests.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 likewindow, document, localStorage:
3. Optimize Performance
UseBEFORE_APP_SERIALIZED for cleanup and optimization:
4. Handle Absolute URLs
Ensure URLs are absolute when rendering on the server:Related APIs
@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
