@@ -20,7 +20,11 @@ import {
2020 OnInit ,
2121 Output ,
2222 ViewEncapsulation ,
23+ Optional ,
24+ Inject ,
25+ PLATFORM_ID ,
2326} from '@angular/core' ;
27+ import { isPlatformBrowser } from '@angular/common' ;
2428import { BehaviorSubject , combineLatest , Observable , Subject } from 'rxjs' ;
2529import { map , shareReplay , take , takeUntil } from 'rxjs/operators' ;
2630
@@ -48,12 +52,6 @@ export const DEFAULT_HEIGHT = '500px';
4852/** Arbitrary default width for the map element */
4953export const DEFAULT_WIDTH = '500px' ;
5054
51- /**
52- * Whether we're currently rendering inside a browser. Equivalent of `Platform.isBrowser`,
53- * but copied over here so we don't have to add another dependency.
54- */
55- const isBrowser = typeof window === 'object' && ! ! window ;
56-
5755/**
5856 * Angular component that renders a Google Map via the Google Maps JavaScript
5957 * API.
@@ -194,6 +192,8 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
194192 private _mapEl : HTMLElement ;
195193 _googleMap ! : UpdatedGoogleMap ;
196194
195+ /** Whether we're currently rendering inside a browser. */
196+ private _isBrowser : boolean ;
197197 private _googleMapChanges ! : Observable < google . maps . Map > ;
198198
199199 private _listeners : google . maps . MapsEventListener [ ] = [ ] ;
@@ -205,8 +205,19 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
205205
206206 private readonly _destroy = new Subject < void > ( ) ;
207207
208- constructor ( private readonly _elementRef : ElementRef ) {
209- if ( isBrowser ) {
208+ constructor (
209+ private readonly _elementRef : ElementRef ,
210+ /**
211+ * @deprecated `platformId` parameter to become required.
212+ * @breaking -change 10.0.0
213+ */
214+ @Optional ( ) @Inject ( PLATFORM_ID ) platformId ?: Object ) {
215+
216+ // @breaking -change 10.0.0 Remove null check for `platformId`.
217+ this . _isBrowser =
218+ platformId ? isPlatformBrowser ( platformId ) : typeof window === 'object' && ! ! window ;
219+
220+ if ( this . _isBrowser ) {
210221 const googleMapsWindow : GoogleMapsWindow = window ;
211222 if ( ! googleMapsWindow . google ) {
212223 throw Error (
@@ -224,7 +235,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
224235
225236 ngOnInit ( ) {
226237 // It should be a noop during server-side rendering.
227- if ( isBrowser ) {
238+ if ( this . _isBrowser ) {
228239 this . _mapEl = this . _elementRef . nativeElement . querySelector ( '.map-container' ) ! ;
229240 this . _setSize ( ) ;
230241
0 commit comments