Solvedangular google maps Clusters are breaking the universal app

Issue description

Just importing the cluster module is breaking the app.

Steps to reproduce and a minimal demo of the problem

I have a map where all its markers are under a cluster. The map is wrapped into a conditional so it renders only on client (not on the server) because there are known issues. However, it still breaks the server side rendering because it tries to access window at some point. This probably happens during module initialization, since the component itself never got instantiated.

Current behavior

Apps using cluster cannot be used in a server side app.

Expected/desired behavior

Apps should not break.

angular & angular-google-maps version

  • @angular/* at 5.0.0
  • @agm/core at 1.0.0-beta.2

Other information

I'm looking for a workaround ATM but, of course, would be nice if users having a Universal app do not have to do anything. We should at least wrap all code with isPlatformBrowser and just list that as a caveat for now, until we figure out how to render maps on the server, if it's even possible.

31 Answers

✔️Accepted Answer

I forked js-marker-clusterer project and made the fix. It prevents initialization MarkerClusterer when window is not defined (on server side rendering). So marker clusterer works correctly in both cases now.

It's published in npm.

js-marker-clusterer dependency should be replaced by js-marker-clusterer-universal to take effect.

Other Answers:

@AoschkA I'm still using the workaround as mentioned in my earlier comment. You can find my example here:

@AoschkA, the repo is just the compiled output, I did not any instructions, you can add it to your package.json like this:

"@agm/js-marker-clusterer": "git+"

When you install it, you will have the js-marker-clusterer without window is undefined errors, and compiled to ES2015 so it's compatible with services like Firebase.

@maksimbykov can you confirm js-marker-clusterer-universal is in your node_modules folder? perhaps it did not fully install, just in case you could run npm i --save js-marker-clusterer-universal. Also something weird seems to be going on with the file paths there. The first one has a double slash // and the second one misses a backslash \ between node_modules and @agm.

Also worth mentioning I did not check if the package still works with angular 6.

My solution base on @mbrezovsky comment above - so

"install:clusterer-universal": "npm i js-marker-clusterer-universal && rm -rf node_modules/js-marker-clusterer && mv node_modules/js-marker-clusterer-universal node_modules/js-marker-clusterer",
"postbuild": "npm run install:clusterer-universal"

and it will be installed after all and replaced.
And SSR works! :)

Related Issues:

angular google maps Angular 4 Universal - SyntaxError: Unexpected token export
Updated the solution to the latest version of babel @philippeboyd @dkmostafa i solved the same issue...
angular google maps How do I resize an icon ?
I am able to use the Icon object against the iconUrl property of the marker My icon looks like: And ...
angular google maps Using Geocoder with GoogleMapsAPIWrapper
How about this - I saw in issue #139 you are providing the ability to access google.maps object via ...
angular google maps how to initilize the module with variable apiKey
Depends on what kind of build tool you are using (e.g angular-cli or custom webpack or ...) and wher...
angular google maps Failed to display maps in page. ERROR TypeError: Object(...) is not a function
Since you are creating Angular: 5 application : Angular: 5.2.11 RxJS 5.x package is installed with i...
angular google maps Setting strokeWeight on a circle throws an error and doesn't add the stroke
@SebastianM any progress with that regarding information @cesarp provided? this issue is quite old.....
angular google maps Lazy-maps-api-loader error
In your app.module.ts imports add: AgmCoreModule.forRoot({ apiKey: 'xxxxxx' Hi @SebastianM ...
angular google maps Directions service
This directive can solve your needs Hi Do you have any plan to support the directions service? ...
angular google maps How to Support "Google Place Autocomplete" (with example)
@Pitouli hello thanks for your solution i have used it and found a better way to support Autocomplet...
angular google maps new @types/googlemaps version generates error:
I managed to Set not ^3.39.12 but 3.39.12 Hello After their latest update of @types/googlemaps (5th ...
angular google maps getNativeMap() and getBounds() Promise never gets resolved neither rejected
Hi all! You do not need a child directive to obtain your native map instance You can get it subscrib...
angular google maps Clusters are breaking the universal app
I forked js-marker-clusterer project and made the fix It prevents initialization MarkerClusterer whe...
angular google maps How to use a Marker Clusterer?
@siegerx3 It's simpler that it sounds first create a custom component or directive: I used a directi...
angular google maps My Maps is gray
I am having this issue currently Hi Everything is well configured (I think) (npm install module + ke...
angular google maps No MouseOver event handler in AgmDataLayer
Paste this code in your code include it in your declarations Issue description There is no MouseOver...
components Error: mat-form-field must contain a MatFormFieldControl.
You have to add to app.module.ts: Bug feature request or proposal: When doing a karma/jasmine test I...
angular Angular5.x lazyLoad problem, undefined is not a function
For others that find this issue via Google as i did: I had the same problem when trying to lazy load...
angular Cyclic dependency error with HttpInterceptor
I resolved simply not setting authService in constructor but getting in the intercept function. ...
angular cli Issue: Cannot read property 'thisCompilation' of undefined
Here is the answer from issues #9793 : npm remove webpack npm install webpack@3.11.0 ...
angular Uncaught Error: Can't resolve all parameters for ...
You are missing an @Injectable() annotation on your ApiService Support requests like these should li...
ngx bootstrap fix(tooltip): tooltip flickering when hover the mouse on the border on the tooltip position
Add pointer-events: none; to the tooltip css problem solved! .tooltip { pointer-events: none; } ...
angular Force reload/refresh current route with RouteReuseStrategy
Hi If you really need to trick the Router into reloading the component on each routerLink click ...
angular Misleading error message "Cannot find a differ supporting object '[object Object]'"
I just ran into the same issue I'm not sure if the recommended solution will work for my case ...
angular cli Error | Could not find plugin "proposal-numeric-separator" | Generating ES5 bundles for differential loading
Try to install @babel/compat-data: ~7.8.0 to devDependencies This is the only workaround which helpe...
angularfire Missing exported members after updating to Angular 6.0.0
It looks like rxjs-compat is missing Use npm i rxjs-compat to install the missing rxjs-compat. ...
sheetjs How to simply export a Worksheet to xlsx?
There are two issues: each object is mapped to a row so if you want a row with name John and city Se...
angular cli Using invalid environment causes error in build (was "ERROR in Cannot read property 'length' of undefined")
Probably not the same cause but we had the same error (Cannot read property 'length' of undefined) w...
angular cli ng serve method ip not open in another machines
The solution is simple Just bind to the correct interface address via ng serve --host= ...
angular cli Incorrect JS paths when ng build -prod is deployed in a subfolder
When you're deploying to non-root path within a domain you'll need to manually update the <base href...
angular update 2 to 4 has problem [ts] Property 'map' does not exist on type 'Observable<Response>'.
I met the same problem with the angular cli 6.0.0 and rxjs 6.1.0 And I solved the problem by replaci...
angular cli Installation error: permission denied for node-sas
sudo npm install -g --unsafe-perm --verbose @angular/cli Installation error (node-sass) with: node v...
angular cli ng build -prod Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory'
Instead of use ng build --prod use ng build --env=prod UPDATE As many of us (thx) have mention this ...
angularfire ERROR TypeError: Object(…) is not a function” using AngularFirestore and firebase
@jamesdaniels Yes npm install rxjs@6 rxjs-compat@6 --save It solved the issue. I want to use firebas...
storybook Webpack 4 support?
I'm getting the below error when running Storybook with a project using Webpack 4; This is preventin...
angular cli Property 'controls' does not exist on type 'AbstractControl'.
Got it! Looked at the docs - FormArray The FormArray class contains the controls property ...
angular Angular2 AoT Compiler Errors
pls try /cc @chuckjaz When I try to compile my project with ngc it throws the below error: Error: Er...
angular cli [Bug] cli needs to be reloaded when a lazy loaded module is added in routes (FIXED)
I recomended change { path: 'user-panel' loadChildren: './user-panel/user-panel.module#UserPanelModu...
prettier Change HTML/JSX formatting to have one attribute/prop per line
The point I am trying to make is that the vertical space matters Wasting it (too much) has negative ...
angularfire @firebase/firestore: Firestore (5.8.0): timestampsInSnapshots
eg: app.module.ts Version info Angular: 7.2.1 Firebase: 5.8.0 AngularFire: 5.1.1 In the newest fireb...
angular cli AOT build fails
I noticed same problem and after npm i enhanced-resolve@3.3.0 I can build with AOT ...
angular cli Cannot find name 'require'. using relative paths...
Ahhh here we go try adding declare var require: any; To src/typings.d.ts I am using a config with re...
components [Table] Add example with dynamic columns
For those interested Bug feature request ...
angular starter How to include Twitter Bootstrap 3 using webpack
I just tested it now Hi I have taken a look at #214 & #215 but these don't really explain how to set...
angular HttpClient.delete() cannot handle a body in its request
It would be great to have body param in .delete() We just migrated our project to HttpClient and for...
ng bootstrap No provider for NgbActiveModal! => "Components as content"
try to provide NgbActiveModal on app.module Bug description: When trying to implement the Modal usin...
angular cli "No NgModule metadata found for 'AppModule'" after Upgrade to Angular 5.1.0 and AngularCli 1.6.0
I got the same error The issue is fixed only when i manually re installed angular/cli The webpack sh...
angularfire 5.2.1 AngularFireAuthGuard causes TypeError: source.lift is not a function
I worked in this way Performing basic docs for AngularFireAuthGuard produces an error related to sou...
angular cli ng serve doesn't listen other than localhost
Works with command ng serve --host OS? Windows 7 8 or 10 Linux (which distribution) Mac OSX ...
angular Http - Observable completed function not firing
Third callback haven't been called when error occures ES6 promises hasn't method finally only then a...