@@ -48,6 +48,12 @@ export const DEFAULT_HEIGHT = '500px';
48
48
/** Arbitrary default width for the map element */
49
49
export const DEFAULT_WIDTH = '500px' ;
50
50
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
+
51
57
/**
52
58
* Angular component that renders a Google Map via the Google Maps JavaScript
53
59
* API.
@@ -200,13 +206,15 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
200
206
private readonly _destroy = new Subject < void > ( ) ;
201
207
202
208
constructor ( private readonly _elementRef : ElementRef ) {
203
- const googleMapsWindow : GoogleMapsWindow = window ;
204
- if ( ! googleMapsWindow . google ) {
205
- throw Error (
206
- 'Namespace google not found, cannot construct embedded google ' +
207
- 'map. Please install the Google Maps JavaScript API: ' +
208
- 'https://developers.google.com/maps/documentation/javascript/' +
209
- 'tutorial#Loading_the_Maps_API' ) ;
209
+ if ( isBrowser ) {
210
+ const googleMapsWindow : GoogleMapsWindow = window ;
211
+ if ( ! googleMapsWindow . google ) {
212
+ throw Error (
213
+ 'Namespace google not found, cannot construct embedded google ' +
214
+ 'map. Please install the Google Maps JavaScript API: ' +
215
+ 'https://developers.google.com/maps/documentation/javascript/' +
216
+ 'tutorial#Loading_the_Maps_API' ) ;
217
+ }
210
218
}
211
219
}
212
220
@@ -215,21 +223,24 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
215
223
}
216
224
217
225
ngOnInit ( ) {
218
- this . _mapEl = this . _elementRef . nativeElement . querySelector ( '.map-container' ) ! ;
219
- this . _setSize ( ) ;
226
+ // It should be a noop during server-side rendering.
227
+ if ( isBrowser ) {
228
+ this . _mapEl = this . _elementRef . nativeElement . querySelector ( '.map-container' ) ! ;
229
+ this . _setSize ( ) ;
220
230
221
- const combinedOptionsChanges = this . _combineOptions ( ) ;
231
+ const combinedOptionsChanges = this . _combineOptions ( ) ;
222
232
223
- this . _googleMapChanges = this . _initializeMap ( combinedOptionsChanges ) ;
224
- this . _googleMapChanges . subscribe ( ( googleMap : google . maps . Map ) => {
225
- this . _googleMap = googleMap as UpdatedGoogleMap ;
233
+ this . _googleMapChanges = this . _initializeMap ( combinedOptionsChanges ) ;
234
+ this . _googleMapChanges . subscribe ( ( googleMap : google . maps . Map ) => {
235
+ this . _googleMap = googleMap as UpdatedGoogleMap ;
226
236
227
- this . _initializeEventHandlers ( ) ;
228
- } ) ;
237
+ this . _initializeEventHandlers ( ) ;
238
+ } ) ;
229
239
230
- this . _watchForOptionsChanges ( ) ;
231
- this . _watchForCenterChanges ( ) ;
232
- this . _watchForZoomChanges ( ) ;
240
+ this . _watchForOptionsChanges ( ) ;
241
+ this . _watchForCenterChanges ( ) ;
242
+ this . _watchForZoomChanges ( ) ;
243
+ }
233
244
}
234
245
235
246
ngOnDestroy ( ) {
0 commit comments