-
Notifications
You must be signed in to change notification settings - Fork 694
Error on npm run serve:ssr #773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
A temporary workaround is to use version 8.0.2 if that's possible. |
any news about this issue ? |
I think that the document used here angular-oauth2-oidc/projects/lib/src/oauth-service.ts Lines 982 to 1008 in ff82dd9
and here angular-oauth2-oidc/projects/lib/src/oauth-service.ts Lines 1302 to 1338 in ff82dd9
document references in the file.
|
Replaced document by this.document this uses the injected service instead of the native object.
hi @kristofdegrave , thanks for taking a loot at it 🚀 I used your fix on my local fork and test on my app with a little
|
Seeing your error are you using this library at server side? Our is this still your client that is giving the error? |
There seems to be a setDocument method to set the DOCUMENT in Ivy, normally Ivy takes care of this, but it seems in your case not. https://github.com/angular/angular/blob/e511bfcab586a94cdae1491e0ec194141445578b/packages/core/src/render3/interfaces/document.ts It is a private function exposed in the core package, maybe try to set the document yourself. https://github.com/angular/angular/blob/fb92f5de1adc8dccbb947a09b34cb1bcc6b41e7f/packages/core/src/core_render3_private_export.ts#L257 |
this error occurs at build time when building with ssr (
i will try to check if i can manage to have something that works this, thanks @kristofdegrave |
What is in your main.ts file? How are you bootstrapping your appmodule? if you are using platformDynamicServer([{
provide: INITIAL_CONFIG,
useValue: {document: '<html><head></head><body><app></app></body></html>'}
}]); This comes from one of the angular test. I asume that the document should contain your |
We setup the project with the Nest plugin. In our We tried adding the INITIAL_CONFIG option, we still have the error. import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
import { INITIAL_CONFIG } from '@angular/platform-server';
if (environment.production) {
enableProdMode();
}
document.addEventListener('DOMContentLoaded', () => {
platformBrowserDynamic([
{
provide: INITIAL_CONFIG,
useValue: {
document: '<html><head></head><body><app></app></body></html>',
},
},
])
.bootstrapModule(AppModule)
.catch((err) => console.error(err));
}); |
I have just create a new ssr projet with nest. And I inject the DOCUMENT token in my AppComponent. |
Did you import the BrowserModule in your AppModule? This can also be a reason. This defines a document ... https://github.com/angular/angular/blob/b01084910266deaa7d8e4424398062e6905199e8/packages/platform-browser/src/browser.ts#L42 |
yes it is :
|
Can you see what the difference is in setup between the original and the new one? |
@kristofdegrave In fact, I have an error. It was not very clear last time I checked this issue.
Not very useful :s The error we have with oidc is about Document with a capitalized D. So it means the DOM object Document is missing. Am I right ? That's normal because we are on the service side. So this class do not exist. But I can not find any documentation about this issue on the Angular Universal documentation :s . Should we import an extra NPM module like domino ? |
Or it would mean that he has issues with the type Document. This is a type that should be defined inside typescript lib
About this subject I know nothing :) |
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:
|
Looks like he can't resolve the type Seems in this post they get the same issue (https://stackoverflow.com/questions/50517138/ng-bootstrap-referenceerror-document-is-not-defined) But if that is the case I would solve it like that, because angular provides it's own solution using the platform-server (https://angular.io/api/platform-server). I would advise to use the |
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 Seems to me there is something specific with angular-oauth2-oidc. If I get some more time I will do some more testing. |
Here is a workaround fix, removing the Document type from the inject token doesn't throw the error. At the moment not sure why, what do you think? See my fork here https://github.com/Exocomp/angular-oauth2-oidc/blob/master/projects/lib/src/oauth-service.ts#L115 However, this creates the side effect of not being able to use intellisense for document. |
Maybe try the following: |
Another try, does your ssr tsconfig contain the https://basarat.gitbook.io/typescript/type-system/lib.d.ts#lib-option |
By default the Angular CLI uses:
|
I have just added this in my tsconfig.server.json, and I still have the error
|
I have the same issue, but as MrFoster pointed out, reverting back to 8.0.2 worked for now. |
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 |
In the mean time, I created a new library with the Document type removed (thanks @Exocomp ). https://www.npmjs.com/package/@elgerm/angular-oauth2-oidc It's based on the latest master but the versioning is still on 9.1. I've tested several stuff that relies on document like refresh token and it all seems to work. Now at least I can use it in production. |
Dumb question, but out of curiosity, is there something in particular that doesn't work when using 8.x? I have a similar requirement of using this library with angular 9 and SSR, and it seems to be working ok. You actually have a workable solution, which is the way to go, but again I was just curious. I know we're supposed to use the matching version, but I'm not sure how to go about finding out what would break, or at least not be guaranteed to work, if using an older package version with the newer angular version. |
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 ***@***.***> wrote:
Dumb question, but out of curiosity, is there something in particular that
doesn't work when using 8.x? I have a similar requirement of using this
library with angular 9 and SSR, and it *seems* to be working ok. You
actually have a workable solution, which is the way to go, but again I was
just curious. I know we're supposed to use the matching version, but I'm
not sure how to go about finding out what would break, or at least not be
guaranteed to work, if using an older package version with the newer
angular version.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#773 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAW3SSSY22DXVA3APMXRWLTRQLQMTANCNFSM4LULXJAQ>
.
|
still no news about this issue ? i've tried some fixes on my side but without any luck yet .. |
Yes no news :( |
I don't use angular-oauth2-oidc but I am getting this error anyway. So it is something else for me. But was I led to this page via google search for "this.debug is not a function" |
We were able to work around this, for now, by adding (global as any).Document = (global as any).Document || {}; at 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. |
obviously not a long term fix, but this workaround works well on our side too, well done @JohannesHuster |
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. |
Thank you sharing your solution with browser APIs @JuanGarciaCarmona :-) Reading through your answer and this issue again, I realized that it might be helpful to separate the 3 different case-sensitive spellings of "Document" used in this issue:
The problem described in this issue has to do with the last spelling ("Document") and seems to have something to do with Dependency Injection in Ivy. When compiling TypeScript to JavaScript this type information would normally not be represented in any way in the JS output. However, in the output from the Angular Compiler (at least when enabling Ivy) there is a reference to // Universal main.js
// ...
OAuthService = Object(tslib__WEBPACK_IMPORTED_MODULE_0__["__decorate"])([ Object(tslib__WEBPACK_IMPORTED_MODULE_0__["__param"])(2, Object(_angular_core__WEBPACK_IMPORTED_MODULE_1__["Optional"])()),
Object(tslib__WEBPACK_IMPORTED_MODULE_0__["__param"])(3, Object(_angular_core__WEBPACK_IMPORTED_MODULE_1__["Optional"])()),
Object(tslib__WEBPACK_IMPORTED_MODULE_0__["__param"])(4, Object(_angular_core__WEBPACK_IMPORTED_MODULE_1__["Optional"])()),
Object(tslib__WEBPACK_IMPORTED_MODULE_0__["__param"])(7, Object(_angular_core__WEBPACK_IMPORTED_MODULE_1__["Optional"])()),
Object(tslib__WEBPACK_IMPORTED_MODULE_0__["__param"])(8, Object(_angular_core__WEBPACK_IMPORTED_MODULE_1__["Inject"])(_angular_common__WEBPACK_IMPORTED_MODULE_2__["DOCUMENT"])),
Object(tslib__WEBPACK_IMPORTED_MODULE_0__["__metadata"])("design:paramtypes", [_angular_core__WEBPACK_IMPORTED_MODULE_1__["NgZone"],
_angular_common_http__WEBPACK_IMPORTED_MODULE_3__["HttpClient"],
OAuthStorage,
ValidationHandler,
AuthConfig,
UrlHelperService,
OAuthLogger,
HashHandler,
Document]) // --> Reference Error since Document is never declared (where this scope could access it)
], OAuthService);
// ... This works for the references above Document, because they are declared in a surrounding scope. This might be an Angular bug, but so far I haven't been able to reproduce the problem with a clean Angular library. I might find the time to look into this more and would then open an issue in the Angular repo, if I manage to reproduce the behavior. |
Replaced document by this.document #773
Thanks for all the information and solutions provided in this thread. Perhaps this also helps: Meanwhile, the Angular Universal team published a new API using jsDOM. This makes using libs that have been primarily written for browser usage easier. You can try this new API out using ng add @nguniversal/common. |
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
OAuthModule.forRoot()
on AppModulenpm run build:ssr && npm run serve:ssr
ornpm 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
The text was updated successfully, but these errors were encountered: