Support Topics Documentation Slack YouTube Blog

Serving JS SDK within Angular Universal is not supported

Hi team,

Does the JS SDK work with Angular Universal? Running the Angular app (which works fine client-side) as Universal gets error during the bootstrap because .initApp is not defined server-side. And because the client initialization is in the AppModule, the app won’t start.
I came up with the following workaround:

  1. In the AppModule, moved the Backendless.initApp( … ) statement inside the class constructor.

  2. Make sure the execution environment is a browser (not a server, which Universal allows)

     constructor(@Inject(PLATFORM_ID) private platformId: Object) {
     if (isPlatformBrowser(this.platformId)) {
         Backendless.initApp(environment.backendless.APP_ID, environment.backendless.API_KEY);
     }
    

    }

The SSR doesn’t really need to request anything to Backendless, at least for my current use case. I’m wondering, though, if the JS SDK works as well for a Node.js app, why are there issues running this Angular + Express + Backendless JS SDK as a Node app.

The only way I could move forward with my app is not relying on the JS SDK in the components/modules that the SSR reaches. However, I don’t know if there’s something the SDK can do/have in order to don’t break the execution.

To be honest, I don’t know if I explained myself and it’s something to worry about. Perhaps just mentioning this workaround in the Angular setup docs would help.

I’m open to provide more details if you need.
Thanks in advance!

Hello @RAFAEL_PEREZ

First of all, thank you for sharing your experience, it might be quite useful for developers who use our JS-SDK with Angular Universal.

Unfortunately, I’m not familiar with Angular Universal and don’t know why it doesn’t work, but the JS-SDK works well in both Browser and Nodejs ENV.

Perhaps, the Angular build system mutates our JS-SDK while composing dist files.

let’s try to figure out what’s going on there, for that could you please answer the questions below:

  • what JS_SDK version do you use?
  • how do import it, using require or es6 import?
  • could you please try to print the whole Backendless namespace right before calling initApp

Regards, Vlad