-
Notifications
You must be signed in to change notification settings - Fork 123
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
Problem with import of HttpClientModule overwriting interceptors #174
Comments
Hi @karptonite, The library imports For instance, the current demo uses ngx-progress where custom We need a reproduction for the issue before we try to fix it, can you reproduce the issue using this stackblitz, the library is already loaded. |
I'm working on a repro now. |
This was a bit involved for stackblitz, but here is a repro: If you click the submodule, you'll see that a book title is not loaded in the submodule, because the necessary interceptor is not run. If you remove the import of Regarding only importing for internal use, I don't believe that is what happens when you import modules that have providers--providers are all global, so if you re-provide for I don't actually know, though, whether wiping out the |
Thinking about this further, if you want to be able to use HttpClient without any interference with anything outside of the package, and vice versa, you might need your own provider, your own InjectionToken, that isolates your version of |
I don't think this is necessarily the best way to handle this, but you might take a look at this: |
@karptonite I am checking it now |
@karptonite In your lazy module @NgModule({
imports: [
CommonModule,
RouterModule.forChild(routes),
CustomHttpModule, // <= Add your custom http module
ShareButtonsModule
],
// ... Problem solved! |
The issue still persists, this approach results in multiple instances of the HttpClient. I think it can only be resolved if you make this plugin independent of HttpClient i.e. neither import HttpClientModule nor add HttpClient to providers list in your plugin. In documentation ask users to import HttpModule in their root module. |
@naveedahmed1 @MurhafSousli Yes, I think this issue should be reopened. Importing HttpClientModule in each lazy loaded module is contrary to its intended use, and not an appropriate solution to this problem. The whole point of the CoreModule is that it handles providers (and modules that handle providers) that need only be imported once. See https://angular.io/guide/http#setup-installing-the-module This is something that you can solve by having the user pass a HttpClient instance as done here, https://github.com/ngx-translate/http-loader, or find a way to create inject an isolated instance of HttpClient with its own InjectionToken (I think that would be preferable). However, I wouldn't suggest that asking users to import HttpModule as a solution--HttpModule is deprecated, I believe. |
@karptonite @naveedahmed1 Alright, but I need some time to investigate about this |
@MurhafSousli I hope you are doing well, do you have any update to share? |
@naveedahmed1 probably next week |
Sounds great 😊 |
Meanwhile I have gone through the code, and I think we can safely remove the HttpClientModule import for this plugin. Whats your conclusion @MurhafSousli ? |
@naveedahmed1 Alright, let's remove it |
Awesome! |
That doesn't entirely solve the problem. |
Yes you are right, I would suggest removing that as well, BTW can't we remove the dependency on HttpClientJsonpModule ? |
Even without HttpClientJsonpModule, it is not ideal, since if the main application has interceptors, the requests from ngx-sharebuttons will pass through those interceptors, but it is much better to have that than to interfere with the main application. |
But normally with interceptors, when we have to perform some additional processing on request/response we should check if the request is being sent to our own server or to some external server. For example if we are appending jwt token, we should make sure its not going to a third part server. (I assume this was your concern). |
Yeah, of course that is how we should write interceptors. I'm just saying that having a user's interceptors breaking the package (if they are not well written, or do something unexpected) could be a confusing source of bugs. But again, much better than messing with the interceptors themselves. |
Agreed! |
@karptonite
|
Thanks! A definite improvement! |
Great! Thank you so much @MurhafSousli :) |
I think this issue which was fixed in 4.1.0, now appears again in 6.0.0. @MurhafSousli can you please take a look? |
@naveedahmed1 The code has not changed! the plugin does not import the ngx-sharebuttons/projects/core/src/lib/share.module.ts Lines 13 to 22 in 27ce548
|
I just found this: Its importing |
That's right! I opened an issue for this #297 |
Awesome, thanks, I hope this can be fixed soon :) |
Fixed, this was real quick , great job bro! |
I'm submitting a ... (check one with "x")
Versions
Repro steps
import
ShareDirectiveModule
in a module (I've only seen it in a lazy-loaded module, but I don't think it is required), when HTTP_INTERCEPTORS are provided at a higher level (for example, in the root app module).The Issue
The
ShareDirectiveModule
importsHttpClientModule
. According to the angular docs, theHttpClientModule
need only be imported once, in the root of the App. One consequence of re-importing theHttpClientModule
is thatHTTP_INTERCEPTORS
are essentially reset both in theShareDirectiveModule
(which isn't a problem) and for the module which imports theShareDirectiveModule
.In my App, for example, I found that all of the HttpRequests that were initiated in the module that also imported your module (but nothing at a higher level) ran without my interceptors.
I'm not sure if this is the intended behavior for angular, or if it is an angular bug, but there might be an easy fix: instead of having
ShareDirectiveModule
importHttpClientModule
, amend the docs to have users importHttpClientModule
when they import theforRoot()
for your module. You may want to do the same with theHttpClientJsonpModule
, which adds an interceptor--I'm not sure if it is necessary, though.Of course, that means that all of your requests will also pass through my interceptor, but it is my responsibility to write the interceptors in a way that doesn't cause problems for your requests.
Obviously, this is a breaking change, but as the package is written now, it is a problem for users who rely on
HTTP_INTERCEPTORS
.The text was updated successfully, but these errors were encountered: