Conditionally import Module depending on isserver or isbrowser
I am trying to use a third party module that works fine in a pure client side implementation.
In a JavaScriptServices implementation I get this error when I try to import AgmCoreModule...
ReferenceError: window is not defined
My import looks like this...
imports: [
UniversalModule,
AgmCoreModule.forRoot({
apiKey: 'XXX',
libraries: ["places"]
}),
I don't need this module in the server side version. What is the best way to only import this for the browser version? Is this supported?
Thank you
One thing you could do is create a platformImports Array, and add to it conditionally depending on the environment.
let platformImports = [];
if (isBrowser) {
platformImports.push(...[
AgmCoreModule.forRoot({
apiKey: 'XXX',
libraries: ["places"]
})
]);
}
if (isNode) { /* same thing if needed */ }
// then later in the actual import array
imports: [
// use the spread operator (...)
...platformImports
]
That should do the trick for those.
If you need Dependency Injection to provide alternate Services(providers) just do an easier approach:
providers: [
{ provide: StorageService, useClass: isBrowser ? BrowserStorage : ServerStorage }
]
Hope that helps!
(Another option is to split the NgModule file into 2 files, one for Browser and one for Server, and import there separately to boot-client & boot-server, and maybe having a CommonModule one that has everything in it that can be shared between both of them)
Thanks for the response, I like the ideas. I have tried the first one but I am seeing the same problem...
ReferenceError: window is not defined
I guess its because the module tries to use window and universal fails on that as its a browser only thing.
Any other ideas as to what I could do when a module has code that I can't get at like this?
First thing if you could, can you tell the library authors that it's not compatible with Universal or even web-workers because it's using window without testing if it exists first. (They need to abstract it out per platform). It'd be ideal if they could fix it!
But I forgot that the first solution won't work if you're using 1 NgModule file, because you're importing the module itself at the top, which itself uses window (something that doesn't exist on the server). You'll have to split them up to get it to work in that case 😓. You can see an example of platform-split modules here. App.common is everything, while the other 2 are things specific to that platform. Just make sure boot-client imports app.browser.module, and server imports app.server.module
I know it's a bit complicated but splitting things up always is 😷
Worst case scenario, you can disable server side rendering.
Thanks @MarkPieszak for answering this!
Yes thank you for your help on this. You guys are doing some great work.
I have created a request for the third party component to be fixed also... https://github.com/SebastianM/angular2-google-maps/issues/896
Thanks again.
Great! Thank you @bencameron00 😀 I'm hoping soon we release some helpful guidelines and tips for library/component developers so they keep "window" and other problematic things in mind. They don't work in web workers as well, so it's pretty important.
Out of interest I was wondering if this is also a common issue. When importing some third party components I quite often see the below error. Is there something that needs to be changed in the component to make it compatible with universal?
An unhandled exception occurred while processing the request.
Exception: Call to Node module failed with error: TypeError: ctorParameters.map is not a function at ReflectionCapabilities.parameters (C:\XXX\ClientApp\dist\vendor.js:2096:51) at Reflector.parameters (C:\XXX\ClientApp\dist\vendor.js:2298:52) at CompileMetadataResolver.getDependenciesMetadata
An unhandled exception occurred while processing the request.
Exception: Call to Node module failed with error: TypeError: ctorParameters.map is not a function at ReflectionCapabilities.parameters (C:\XXX\ClientApp\dist\vendor.js:2096:51) at Reflector.parameters (C:\XXX\ClientApp\dist\vendor.js:2298:52) at CompileMetadataResolver.getDependenciesMetadata
Not sure what library you're using there but If it's Material it's not currently compatible with Universal yet (without lots of hacks). Also this could be related issue : http://stackoverflow.com/questions/41285889/ctorparameters-map-is-not-a-function-in-angular2-mdl
Hi @MarkPieszak , could you show me an example how you add condition for, by example, app.module.ts? I am trying to deploy the angular universal and I just stuck in an error Error: NotYetImplemented so I am trying to isolate one module by one module to see what happen.