JavaScriptServices icon indicating copy to clipboard operation
JavaScriptServices copied to clipboard

Conditionally import Module depending on isserver or isbrowser

Open bencameron00 opened this issue 9 years ago • 9 comments

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

bencameron00 avatar Feb 07 '17 20:02 bencameron00

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)

MarkPieszak avatar Feb 07 '17 23:02 MarkPieszak

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?

bencameron00 avatar Feb 08 '17 18:02 bencameron00

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.

MarkPieszak avatar Feb 08 '17 19:02 MarkPieszak

Thanks @MarkPieszak for answering this!

SteveSandersonMS avatar Feb 09 '17 10:02 SteveSandersonMS

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.

bencameron00 avatar Feb 10 '17 10:02 bencameron00

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.

MarkPieszak avatar Feb 10 '17 13:02 MarkPieszak

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

bencameron00 avatar Feb 11 '17 13:02 bencameron00

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

MarkPieszak avatar Feb 11 '17 16:02 MarkPieszak

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.

thovo avatar Feb 13 '18 17:02 thovo