Skip to content

Commit 4a587a1

Browse files
authored
fix(google-maps): server-side rendering error from map clusterer (#21049)
We have a guard around trying to initialize the clusterer on the server, but there's some more logic in `ngAfterContentInit` that will end up throwing if it hasn't been initialized. Also adds a server-side rendering check.
1 parent 6612167 commit 4a587a1

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ export class MapMarkerClusterer implements OnInit, AfterContentInit, OnDestroy {
6666
private readonly _eventManager = new MapEventManager(this._ngZone);
6767
private readonly _destroy = new Subject<void>();
6868

69+
/** Whether the clusterer is allowed to be initialized. */
70+
private readonly _canInitialize: boolean;
71+
6972
@Input()
7073
get ariaLabelFn(): AriaLabelFn {
7174
return this.markerClusterer ? this.markerClusterer.ariaLabelFn : () => '';
@@ -182,10 +185,12 @@ export class MapMarkerClusterer implements OnInit, AfterContentInit, OnDestroy {
182185
*/
183186
markerClusterer?: MarkerClusterer;
184187

185-
constructor(private readonly _googleMap: GoogleMap, private readonly _ngZone: NgZone) {}
188+
constructor(private readonly _googleMap: GoogleMap, private readonly _ngZone: NgZone) {
189+
this._canInitialize = this._googleMap._isBrowser;
190+
}
186191

187192
ngOnInit() {
188-
if (this._googleMap._isBrowser) {
193+
if (this._canInitialize) {
189194
this._combineOptions().pipe(take(1)).subscribe(options => {
190195
// Create the object outside the zone so its events don't trigger change detection.
191196
// We'll bring it back in inside the `MapEventManager` only for the events that the
@@ -219,7 +224,9 @@ export class MapMarkerClusterer implements OnInit, AfterContentInit, OnDestroy {
219224
}
220225

221226
ngAfterContentInit() {
222-
this._watchForMarkerChanges();
227+
if (this._canInitialize) {
228+
this._watchForMarkerChanges();
229+
}
223230
}
224231

225232
ngOnDestroy() {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,4 +426,10 @@ <h2>Google Map</h2>
426426
<map-traffic-layer></map-traffic-layer>
427427
<map-transit-layer></map-transit-layer>
428428
<map-bicycling-layer></map-bicycling-layer>
429+
430+
<map-marker-clusterer imagePath="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m">
431+
<map-marker [position]="{lat: 24, lng: 12}"></map-marker>
432+
<map-marker [position]="{lat: 12, lng: 24}"></map-marker>
433+
<map-marker [position]="{lat: 12, lng: 12}"></map-marker>
434+
</map-marker-clusterer>
429435
</google-map>

0 commit comments

Comments
 (0)