angular-oauth2-oidc: Error on npm run serve:ssr
Describe the bug
I’m trying to build and serve a local app in ssr mode but npm run server:ssr crash.
To Reproduce Steps to reproduce the behavior:
npm install angular-oauth2-oidc --save- add
OAuthModule.forRoot()on AppModule npm run build:ssr && npm run serve:ssrornpm run dev:ssr
Expected behavior Node Express server listening on http://localhost:4000
Additional context
ReferenceError: Document is not defined at Module../node_modules/angular-oauth2-oidc/fesm2015/angular-oauth2-oidc.js
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 17
- Comments: 36 (11 by maintainers)
Commits related to this issue
- Replaced document by this.document #773 Replaced document by this.document this uses the injected service instead of the native object. — committed to kristofdegrave/angular-oauth2-oidc by kristofdegrave 4 years ago
- Workaround fix for Angular 9 Document is not defined For details see https://github.com/manfredsteyer/angular-oauth2-oidc/issues/773 — committed to Exocomp/angular-oauth2-oidc by Exocomp 4 years ago
- Merge pull request #787 from kristofdegrave/patch-1 Replaced document by this.document #773 — committed to manfredsteyer/angular-oauth2-oidc by manfredsteyer 4 years ago
I created a new Angular 9 project and added SSR, keeping everything default.
Then as a test, I injected the DOCUMENT token in AppComponent and use it in ngOnInit(). I could not reproduce the problem.
However, as soon as I added angular-oauth2-oidc and
OAuthModule.forRootin AppModule I got the error (Document is not defined) on build.Seems to me there is something specific with angular-oauth2-oidc. If I get some more time I will do some more testing.
still no news about this issue ? i’ve tried some fixes on my side but without any luck yet …
We were able to work around this, for now, by adding
Documentto the global object when running on the server. For example, addat the beginning of your main.server.ts. Be careful to add this in a file that is included only on the server.
I have also added a PR for this #853.
I have just added this in my tsconfig.server.json, and I still have the error
I’m getting the error as well, working with angular-oauth2-oidc v9 (Angular 9.1.0). At the moment now sure why.
The line it is referring to in main.js:
Hi there,
I was evaluating whether to use this library in my solution because I am using (the) other oidc client option and facing some weird issues regarding token storage at server side (and protected modules CanLoad)…
Well, for all of you that are facing issues with document, window and any other browser APIs here is my advice:
As an example (a working example indeed):
Note that this code is using i18n but I left the original request handler.
I hope it helps, I spent a huge amount of time fixing my own issues after Angular 9 migration.
obviously not a long term fix, but this workaround works well on our side too, well done @JohannesHuster
Not sure, I started using it with v9, then tried 8.0.3 and got lots of errors. So that made me fork and “fix” the v9 version
On Thu, 7 May 2020 at 18:43, mikg2003 notifications@github.com wrote:
Same here, can’t go back to 8.x because I’m on angular ivy / 9 and I can’t release this because we’re using SSR
I think that the document used here https://github.com/manfredsteyer/angular-oauth2-oidc/blob/ff82dd9b7fc81a1a001730a4013c639061fe57e9/projects/lib/src/oauth-service.ts#L982-L1008 here https://github.com/manfredsteyer/angular-oauth2-oidc/blob/ff82dd9b7fc81a1a001730a4013c639061fe57e9/projects/lib/src/oauth-service.ts#L1180 and here https://github.com/manfredsteyer/angular-oauth2-oidc/blob/ff82dd9b7fc81a1a001730a4013c639061fe57e9/projects/lib/src/oauth-service.ts#L1302-L1338 should be prefixed by this.document. The DOCUMENT is injected correctly, but not applied on all
documentreferences in the file. https://github.com/manfredsteyer/angular-oauth2-oidc/blob/ff82dd9b7fc81a1a001730a4013c639061fe57e9/projects/lib/src/oauth-service.ts#L115