liveBook · Manning

isPlatformServer

As explained in Pro Angular 16

isPlatformServer is an Angular function that determines whether an application is running on a server platform.

isPlatformServer determines whether an Angular application is executing on a server platform. SSR code uses this distinction to avoid browser-only behavior and select environment-appropriate implementations. section 27.4.2

Wrap platform detection in a service

Inject PLATFORM_ID and expose a small application-facing property: `constructor(@Inject(PLATFORM_ID) private platformId: Object) {}` followed by `get isServer() { return isPlatformServer(this.platformId); }`. Register PlatformService, then use `ps.isServer` where runtime behavior differs.

Choose an SSR-safe request URL

A REST data source selects `http://localhost:3500/products` on the server and uses the browser's `location.hostname` in the browser. This confines direct browser API access to browser execution. section 27.4.2

How Pro Angular 16 covers it

How deep it goes
Defines platform detection and demonstrates a service wrapper, but does not descend into the function's implementation internals.
Examples
Demonstrates a PlatformService wrapper and platform-specific REST URLs using localhost or the browser location object.
What it assumes
Assumes Angular services and dependency injection while showing how PLATFORM_ID supplies the value needed for platform detection.
What you can do after
Apply it via Angular dependency injection to select server-safe or browser-specific behavior in an SSR application.
How it connects
Connects server-side rendering with PLATFORM_ID, dependency injection, browser APIs, and REST data requests.

This explainer was generated by Manning's knowledge graph from the text of Pro Angular 16. The section links point to the parts of the book it draws on.