moribvndvs / ng2-idle

Responding to idle users in Angular (not AngularJS) applications.
https://moribvndvs.github.io/ng2-idle
Apache License 2.0
315 stars 128 forks source link

ng2-idle server pre rendering issue #77

Closed Aravinthan27 closed 2 years ago

Aravinthan27 commented 6 years ago

I am using ng2-idle in my Project. It is working fine when running the project using ng serve.

But when i try to take build usingnpm start causing the error

"ReferenceError: document is not defined"

fadelguy commented 6 years ago

There i s a solution for this problem?

froodley commented 6 years ago

Yes, @ng-idle/core does not work with Angular universal / platform-server as of v2.0.0-beta.15 and Angular 5.2.9.

The problem code is in node_modules/@ng-idle/core/bundles/core.umd.js, ~175

The reference to document fails on the server

You need to check if document exists before referencing it. Since the server code would never need to time out, I would think ideally if it's invoked on the server it should just give up and not set up any timers, etc.

froodley commented 6 years ago

The same situation exists in node_modules/@ng-idle/core/src/windowinterruptsource.js, must check for window before accessing it

moribvndvs commented 4 years ago

If you are using SSR/Universal, following this change you should no longer use DEFAULT_INTERRUPTSOURCES.

Instead, create your own instances of desired sources, or use createDefaultInterruptSources({ ssr: !isPlatformBrowser(platformId) }). The ssr option (which can be used alongside the other EventTargetInterruptOptions you desire) should be false when running in the browser, and true when running on the server. You can use isPlatformBrowser or isPlatformServer to determine which the app is running in.

DEFAULT_INTERRUPTSOURCES is equivalent to createDefaultInterruptSources({ ssr: false }), meaning they will not run on SSR/Universal (which is the current/default behavior).