Skip to content

Commit 0b0e98c

Browse files
crisbetojelbourn
authored andcommitted
fix(google-maps): error during server-side rendering (#17744)
Fixes the `google-map` component throwing errors during server-side rendering.
1 parent 56fe791 commit 0b0e98c

File tree

4 files changed

+35
-19
lines changed

4 files changed

+35
-19
lines changed

src/google-maps/google-map/google-map.ts

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ export const DEFAULT_HEIGHT = '500px';
4848
/** Arbitrary default width for the map element */
4949
export const DEFAULT_WIDTH = '500px';
5050

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+
5157
/**
5258
* Angular component that renders a Google Map via the Google Maps JavaScript
5359
* API.
@@ -200,13 +206,15 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
200206
private readonly _destroy = new Subject<void>();
201207

202208
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+
}
210218
}
211219
}
212220

@@ -215,21 +223,24 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
215223
}
216224

217225
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();
220230

221-
const combinedOptionsChanges = this._combineOptions();
231+
const combinedOptionsChanges = this._combineOptions();
222232

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;
226236

227-
this._initializeEventHandlers();
228-
});
237+
this._initializeEventHandlers();
238+
});
229239

230-
this._watchForOptionsChanges();
231-
this._watchForCenterChanges();
232-
this._watchForZoomChanges();
240+
this._watchForOptionsChanges();
241+
this._watchForCenterChanges();
242+
this._watchForZoomChanges();
243+
}
233244
}
234245

235246
ngOnDestroy() {

src/universal-app/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ ng_module(
2020
],
2121
deps = [
2222
"@npm//@angular/platform-server",
23+
"//src/google-maps",
2324
"//src/youtube-player",
2425
] + CDK_TARGETS + CDK_EXPERIMENTAL_TARGETS + MATERIAL_TARGETS + MATERIAL_EXPERIMENTAL_TARGETS,
2526
)

src/universal-app/kitchen-sink/kitchen-sink.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,5 +381,7 @@ <h2>Virtual scroll</h2>
381381
</cdk-virtual-scroll-viewport>
382382

383383
<h2>YouTube player</h2>
384-
385384
<youtube-player videoId="dQw4w9WgXcQ"></youtube-player>
385+
386+
<h2>Google Map</h2>
387+
<google-map height="400px" width="750px"></google-map>

src/universal-app/kitchen-sink/kitchen-sink.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {MatFormFieldModule} from '@angular/material/form-field';
3838
import {MatSortModule} from '@angular/material/sort';
3939
import {MatStepperModule} from '@angular/material/stepper';
4040
import {YouTubePlayerModule} from '@angular/youtube-player';
41+
import {GoogleMapsModule} from '@angular/google-maps';
4142
import {Observable, of as observableOf} from 'rxjs';
4243

4344
export class TableDataSource extends DataSource<any> {
@@ -140,6 +141,7 @@ export class KitchenSink {
140141

141142
// Other modules
142143
YouTubePlayerModule,
144+
GoogleMapsModule,
143145
],
144146
declarations: [KitchenSink, TestEntryComponent],
145147
exports: [KitchenSink, TestEntryComponent],

0 commit comments

Comments
 (0)