Angular: When HttpInterceptor doesn’t work with lazy loaded modules
Tags: angular, httpinterceptor
Published: 2019-06-04 16:47:58


![](https://cdn-images-1.medium.com/max/1600/1*smjDpbbOq5FxOBDOWoG6eg.png)
<span class="figcaption_hack">Http Interceptor working with a Lazy Loaded Module</span>

If you’re doing Angular the right way, you should be using some kind of httpinterceptor. An [HttpInterceptor](https://angular.io/api/common/http/HttpInterceptor) provides a standard way to intercept HTTP requests and responses and apply custom transformations as suits your application needs. If you’re not familiar with the different use cases for HttpInterceptor, [this article from Angular In Depth](https://blog.angularindepth.com/top-10-ways-to-use-interceptors-in-angular-db450f8a62d6) will help you understand how you can apply it to your application, and you should. To summarize, you can use interceptors to, amongst other things:

* Replace/ urls on the fly
* Hide/Show a loader when loading things
* Add request headers (CORS)
* Handle errors
* Log http operations
* Create a fake backend
* Authentication

As you can see, it’s a pretty nifty way to handle some classic requirements of most modern apps.

### Setting up an HttpInterceptor

Creating an HttpInterceptor is very [straight forward](https://itnext.io/angular-tutorial-implement-refresh-token-with-httpinterceptor-bfa27b966f57). In general, you want to create you interceptor as a singleton, so [it can be used across your application and only instantiated once by Angular’s DI](https://angular.io/guide/singleton-services). This involves for your average Angular app:

* Adding the provider definition to the `providers` configuration of `CoreModule`
* Importing `HttpClientModule`into the `imports` configuration of `CoreModule`
* Making sure that no other module imports `HttpClientModule` in your application
once `CoreModule` is imported in your `AppModule`. If you don’t use CoreModule ([Best practice warning, you
should](https://medium.com/@benmohamehdi/angular-best-practices-coremodule-vs-sharedmodule-25f6721aa2ef)), you can directly import `HttpClientModule` and your interceptor provider definition into `AppModule.`

One of the key requirements here is that `HttpClientModule` is only imported once in your application. The reason why you would want this module to be only be imported once is that this is the module from which we import the`HttpClient`service, which is used throughout the application to make HTTP calls. This service is set to handle interceptors should they exist but all interceptors are set up to wok on that single instance of the HttpClient service. Understanding this plays an important role in the next section.

### Lazy Loaded Modules and HttpInterceptor

[Lazy loaded modules](https://angular.io/guide/lazy-loading-ngmodules) is a performance related Angular feature that allows modules to only be loaded when their route is visited. This allows your application to bootstrap a lot faster by initially only downloading just the necessary code to get the user to the first actionable screen of your application. One characteristic of lazy loaded modules is that any service declared in the `providers` configuration is only available to that module, in Angular speak: “[providers of lazy-loaded modules are module-scoped](https://angular.io/guide/ngmodule-faq#why-is-a-service-provided-in-a-lazy-loaded-module-visible-only-to-that-module)*”.*
Even more specifically:

> When the router creates a component within the lazy-loaded context, Angular
> prefers service instances created from these providers to the service instances of the application root injector.

### It doesn’t work!

In my case, I set up a token interceptor that was supposed to be shared
application wide and adds an authentication token to all API bound requests. Debugging showed my that for some calls, the interceptor was getting used, but any other HTTP request from within any of my feature modules was not going through the interceptor at all. Through some more debugging and research, which included some interesting side clarifications on CORS preflight request specification (Did you know that one of a condition for a request to be preflighted [is if it sets custom headers in the request](https://stackoverflow.com/questions/39075370/preflight-request-triggers-based-on-content-type) (e.g. the request uses a custom header such as `x-auth-token`?), I came to better understand why my interceptor was not getting hit.

### The Why

It came down to having one of the other NPM packages I use in my lazy loaded modules ALSO importing the `HttpClientModule` . Everytime that happens, a new instance of the `HttpClient` service is injected in the module that of course has not been configured to use the interceptor configured in the `CoreModule` .
I could not find a good workaround for this situation short of redeclaring the interceptor provider configuration on each lazy loaded module as a short term workaround while I figure out which package is importing the `HttpClientModule` or if Angular accounted for this use case and allows modules to be loaded only if they have not been previously been.

### The Fix

As of now, I don’t have a clear way to fix this short of not using the libraries that import `HttpClientModule` or adding the module-scoped definition of the interceptor provider to each lazy loaded feature.

I’d appreciate if you ran into this issue or similar and found a way to solve it
in an elegant way to share it in the comments.