From 16798b3f6597d6d72941ce7eae7b00ef7e07ea05 Mon Sep 17 00:00:00 2001 From: Thilak Rao Date: Wed, 19 Sep 2018 19:59:03 +0200 Subject: [PATCH] Angular Universal Compatibility for Server Side Rendering - Conditionally mock the reference to the window object while rendering on the server side - Removed unused import specifier tap --- .../src/lib/angular-token.service.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/projects/angular-token/src/lib/angular-token.service.ts b/projects/angular-token/src/lib/angular-token.service.ts index 30fa7d29..920a9221 100755 --- a/projects/angular-token/src/lib/angular-token.service.ts +++ b/projects/angular-token/src/lib/angular-token.service.ts @@ -1,9 +1,10 @@ -import { Injectable, Optional, Inject } from '@angular/core'; +import { Injectable, Optional, Inject, PLATFORM_ID } from '@angular/core'; import { ActivatedRoute, Router, CanActivate } from '@angular/router'; import { HttpClient } from '@angular/common/http'; +import { isPlatformServer } from '@angular/common'; import { Observable, fromEvent, interval } from 'rxjs'; -import { pluck, filter, tap, share, finalize } from 'rxjs/operators'; +import { pluck, filter, share, finalize } from 'rxjs/operators'; import { ANGULAR_TOKEN_OPTIONS } from './angular-token.token'; @@ -53,9 +54,20 @@ export class AngularTokenService implements CanActivate { constructor( private http: HttpClient, @Inject(ANGULAR_TOKEN_OPTIONS) config: any, + @Inject(PLATFORM_ID) private platformId: Object, @Optional() private activatedRoute: ActivatedRoute, @Optional() private router: Router ) { + if (isPlatformServer(platformId)) { + window = { + open: () => null, + location: { + href: '/', + origin: '/' + } + } as Window; + } + const defaultOptions: AngularTokenOptions = { apiPath: null, apiBase: null,