Overview
ThePlatformService provides a safe way to detect whether the application is running in the browser or on the server. This is essential for server-side rendering (SSR) support, preventing errors when trying to access browser-only APIs like window or localStorage.
This service is automatically provided at the root level and can be injected anywhere in your application.
Class definition
src/app/shared/services/utils/platform.service.ts
Properties
Returns
true when the application is running on the server (SSR mode).Returns
true when the application is running in the browser.Usage
Check platform before accessing browser APIs
UsePlatformService to conditionally execute browser-only code:
Prevent localStorage errors in SSR
Alternative: LocalRepository
For localStorage operations, prefer using theLocalRepository service which already handles SSR safely:
Implementation details
The service uses Angular’sPLATFORM_ID token and isPlatformServer helper to detect the runtime environment. When running on the server, it also disables console logging to prevent server-side console pollution.
Related APIs
- LocalRepository - SSR-safe localStorage wrapper (preferred for storage operations)
- AuthStore - Uses LocalRepository for SSR-safe state persistence