Sub topic 6
How interceptor order affects requests and responses
This chapter assumes you can write a functional interceptor and register it with withInterceptors. It also assumes you know basic RxJS pipe and tap. Here you learn how the order of the array decides what each interceptor sees.
The rule
Interceptors form a chain. The array you pass to withInterceptors sets the order:
- A request passes through the interceptors from first to last in the array. The last one hands it to the backend.
- A response comes back through them in the reverse order, from last to first.
Each interceptor wraps the ones after it, like layers of an onion. The first interceptor is the outermost layer. It sees the request first and the response last.
Seeing the order in code
Two interceptors that log when the request arrives and when the response completes make the order visible.
import { HttpInterceptorFn } from '@angular/common/http';
import { tap } from 'rxjs';
export const first: HttpInterceptorFn = (req, next) => {
console.log('first: request');
return next(req).pipe(tap({ complete: () => console.log('first: response') }));
};
export const second: HttpInterceptorFn = (req, next) => {
console.log('second: request');
return next(req).pipe(tap({ complete: () => console.log('second: response') }));
};Register them in this order:
import { bootstrapApplication } from '@angular/platform-browser';
import { provideHttpClient, withInterceptors } from '@angular/common/http';
import { AppComponent } from './app/app.component';
import { first, second } from './app/order.interceptors';
bootstrapApplication(AppComponent, {
providers: [provideHttpClient(withInterceptors([first, second]))],
});After one successful request, the console shows:
first: requestsecond: requestsecond: responsefirst: response
If you swap the array to [second, first], every line swaps with it.
Why order changes behavior
Order matters whenever one interceptor depends on what another did. Consider the two interceptors from earlier chapters, authInterceptor and loggingInterceptor.
| Array order | What the logger sees |
|---|---|
| `[authInterceptor, loggingInterceptor]` | The request with the `Authorization` header, because auth already cloned it |
| `[loggingInterceptor, authInterceptor]` | The original request without the header |
The first order is usually what you want when logs should show what was actually sent. The second keeps tokens out of your logs. Neither is wrong, but you should choose deliberately.
The same applies to the response side. Timing in a logger placed first includes the work done by every interceptor after it. Timing in a logger placed last measures little more than the network call.
Placing error handling and retry
An error interceptor placed first catches errors from the whole chain, including errors thrown by interceptors after it. One placed last only sees errors coming straight from the backend.
Retry needs extra care. An operator like retry resubscribes to the observable returned by next. That reruns the interceptors after it in the array, but not the ones before it. If a retry interceptor sits before authInterceptor, each retry builds the header again and picks up a fresh token. If it sits after, each retry resends the same request with the same header.
Exercise
You register withInterceptors([errorInterceptor, authInterceptor, loggingInterceptor]). In what order does a response pass through them, and does loggingInterceptor see the Authorization header on the request?
Show answer
The response passes through loggingInterceptor, then authInterceptor, then errorInterceptor, which is the reverse of the array. loggingInterceptor does see the Authorization header, because authInterceptor comes earlier in the array and has already cloned the request with the header before passing it on.