Angular: When HttpInterceptor doesn’t work with lazy loaded modules
If you’re doing Angular the right way, you should be using some kind of http interceptor. An 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 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. 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. This involves for your lambda Angular app;
- Adding the provider definition to the
providers
configuration ofCoreModule
- Importing
HttpClientModule
into theimports
configuration ofCoreModule
- Making sure that no other module imports
HttpClientModule
in your application onceCoreModule
is imported in yourAppModule
. If you don’t use CoreModule (Best practice warning, you should), you can directly importHttpClientModule
and your interceptor provider definition intoAppModule.
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 theHttpClient
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 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”. 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 (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.
I am CTO at AKIL Technologies, an Abidjan, Ivory Coast tech startup where I lead a team of Angular and React devs ready to contribute on interesting projects. Give us a toot if you need to beef up your front end teams!