ngx-md icon indicating copy to clipboard operation
ngx-md copied to clipboard

Import of HttpClientModule within ngx-md clobbers existing interceptors

Open nuwang opened this issue 5 years ago • 5 comments

Ngx-md imports the HttpClientModule internally. However, this will overwrite existing application interceptors, such as the XSRFInterceptor, which can cause module wide misbehaviour.

For a similar issue in another module, please see this issue: https://github.com/MurhafSousli/ngx-sharebuttons/issues/174

and the corresponding fix: https://github.com/MurhafSousli/ngx-sharebuttons/commit/a503936c754274948bc9aa92ee5b8d94e71fc503#diff-09baae46def54c6afaeed08d24cf8bc2

nuwang avatar Oct 20 '18 12:10 nuwang

Why markdown library even needs HttpClientModule?

vajda avatar Nov 30 '18 07:11 vajda

@vajda We need HttpClientModule for loading external resource when

<ngx-md [path]="'/path/to/code.cpp'"></ngx-md>

@nuwang We used fetch API to get remote content but to fix https://github.com/dimpu/ngx-md/issues/117 issue we had to use HttpClientModule instead.

But I'm trying to understand the issue here. Could you create a https://stackblitz.com/ demo app?

dimpu avatar Nov 30 '18 17:11 dimpu

@dimpu Thanks for looking into this. The issue is that the HttpClientModule is only supposed to be imported once per application (as stated in the Angular docs). However, ngx-md imports the HttpClientModule internally, which has bizarre, unintended side-effects. The recommended solution to this issue is to simply not import the HttpClientModule within ngx-md. Instead, users of ngx-md should manually import HttpClientModule at the app level.

For a more full discussion of this issue, see here: https://github.com/angular/angular/issues/20575. I don't find that Angular's design decision behind this is satisfactorily explained, but nevertheless, the issue itself is clearly described.

In all these cases, the fix is for modules such as ngx-md to not import the HttpClientModule.

nuwang avatar Nov 30 '18 20:11 nuwang

@dimpu Is this issue being solved?

I spent hours on trying to find where the problem was. It can break your whole app if you for example use interceptor to attach authorization headers to your http request.

Correct way to create a library which needs HttpClientModule is to list it as peer dependency and left it on the user to provide in his module.

I can offer PR if you want, thanks

mhamern avatar Mar 01 '19 16:03 mhamern

@mhamern HttpClientModule is part of @angular/common which is already a peer-dependency. But yes of course if you have better way of solving this. PR would be great. Thanks.

dimpu avatar Mar 01 '19 17:03 dimpu