AlisonVilela/ngx-zendesk-webwidget

widget throws timeout in angular 9 SSR

coanpape opened this issue · 3 comments

I am working on an angular universal app that has been using the ngx-zendesk-webwidget ver 1.x and upgraded from angular 7.x to angular 9.x. If the widget is configured to use lazy loading the Server Side Rendered pages are rendered and sent to the browser as one would expect, however if the config is setup with lazyLoading false ...

export class ZendeskConfig extends NgxZendeskWebwidgetConfig
{ lazyLoad: boolean = false; }

the page does not render until a timeout occurs on an unfulfilled promise...
with lazyLoad = true the error is thrown later to the server console.
Apparently some resource being loaded is unavailable on the server.
The user would benefit greatly if the library detected isPlatformBrowser = false, or accepted it as a parameter, and then bypassed the offending resource loading.

I have isolated the behavior as occurring only when the dependency injection in app.component.ts is run for
constructor(
private zendesk: NgxZendeskWebwidgetService,

if this DI is commented out the error does not occur. The presence of

NgxZendeskWebwidgetModule.forRoot(ZendeskConfig),

in app.module's Imports section does not cause the issue, absent the app.component code.

the error is:
ERROR Error: Uncaught (in promise): Error: timeout
Error: timeout
at Timeout. (D:\project\dist\server\main.js:231014:24)
at ZoneDelegate../node_modules/zone.js/dist/zone-node.js.ZoneDelegate.invokeTask (D:\project\dist\server\main.js:257357:35)
at Object.onInvokeTask (D:\project\dist\server\main.js:102217:33)

I adopted an approach from #40 (comment)

with lazyLoad = true ...
and the app.component constructor:

constructor(
    @Inject(PLATFORM_ID) private platformId: Object,
    private zendesk: NgxZendeskWebwidgetService
}
 ngOnInit() {
    if (isPlatformBrowser(this.platformId)) {
       this.zendesk.initZendesk(new ZendeskConfig())
}}

it feels a bit odd that the specified config is being passed to both the
NgxZendeskWebwidgetModule.forRoot(ZendeskConfig) (in app.module)
and this new call to initZendesk,
but this gets past the issue

In fact it shouldn't be mandatory in init