Full answer
HttpContext. Interceptors sometimes need to know something about one request: skip the auth header for a public endpoint, or retry this one three times. Headers are for the server, not for your own code. HttpContext is a typed bag for exactly this. Define new HttpContextToken<boolean>(() => false) (the function gives the default), call http.get(url, { context: new HttpContext().set(SKIP_AUTH, true) }), and read it in the interceptor with req.context.get(SKIP_AUTH). The context survives clone().
withFetch(). provideHttpClient(withFetch()) uses the fetch API instead of XMLHttpRequest. It works in Node for SSR without extra setup and is recommended for server rendering. Limits: fetch has no upload progress events, so a progress bar for uploads needs XHR (the default backend). Download progress works with reportProgress: true and observe: 'events'.
Cancellation. Unsubscribing from an HTTP Observable aborts the request. This is why switchMap cancels outdated calls, and why the async pipe cancels on destroy. For fetch-based code outside HttpClient use AbortController.
SSR transfer cache. When hydration is on, the server's GET and HEAD responses are serialised into the HTML. The browser's first identical request is answered from this cache and not sent again. By default requests with an Authorization header or a Proxy-Authorization header are not cached, and POST requests are skipped. Tune it with withHttpTransferCacheOptions({ includePostRequests, includeRequestsWithAuthHeaders, filter }) or turn it off with withNoHttpTransferCache(). Make sure that personalised responses are not cached into HTML that a CDN may store for everybody.
Other points: HttpParams and HttpHeaders are immutable, responseType changes the result type ('text', 'blob'), and observe: 'response' gives headers and status.
// file: tokens.ts
import { HttpContextToken } from '@angular/common/http';
export const SKIP_AUTH = new HttpContextToken<boolean>(() => false);
export const RETRIES = new HttpContextToken<number>(() => 0);
// file: interceptors.ts
import { HttpInterceptorFn } from '@angular/common/http';
import { retry } from 'rxjs';
import { RETRIES, SKIP_AUTH } from './tokens';
export const authInterceptor: HttpInterceptorFn = (req, next) =>
req.context.get(SKIP_AUTH)
? next(req)
: next(req.clone({ setHeaders: { Authorization: 'Bearer demo-token' } }));
export const retryInterceptor: HttpInterceptorFn = (req, next) =>
next(req).pipe(retry(req.context.get(RETRIES)));
// file: news.service.ts
import { HttpClient, HttpContext } from '@angular/common/http';
import { Injectable, inject } from '@angular/core';
import { Observable } from 'rxjs';
import { RETRIES, SKIP_AUTH } from './tokens';
@Injectable({ providedIn: 'root' })
export class NewsService {
private readonly http = inject(HttpClient);
publicHeadlines(): Observable<string[]> {
const context = new HttpContext().set(SKIP_AUTH, true).set(RETRIES, 2);
return this.http.get<string[]>('/api/public/headlines', { context });
}
}
// file: app.config.ts
import { provideHttpClient, withFetch, withInterceptors } from '@angular/common/http';
import { ApplicationConfig } from '@angular/core';
import { provideClientHydration, withHttpTransferCacheOptions } from '@angular/platform-browser';
import { authInterceptor, retryInterceptor } from './interceptors';
export const appConfig: ApplicationConfig = {
providers: [
provideHttpClient(withFetch(), withInterceptors([authInterceptor, retryInterceptor])),
provideClientHydration(
withHttpTransferCacheOptions({
includePostRequests: false,
includeRequestsWithAuthHeaders: false,
filter: (req) => !req.url.includes('/api/private'),
}),
),
],
};Angular 20.3, type-checked. The retry count is read from the context in the same interceptor.
Key points
- `HttpContextToken` plus `HttpContext` pass typed per-request options to interceptors.
- `withFetch()` is recommended for SSR but has no upload progress.
- Unsubscribing cancels the request.
- The SSR transfer cache serves the browser's first GET from server data. Tune it with `withHttpTransferCacheOptions`.
Common mistakes
- Using a custom header such as `X-Skip-Auth` to signal the interceptor, which also sends it to the server.
- Mutating `HttpParams` and expecting the change in place. It returns a new object.
- Letting personalised data go through the transfer cache into cacheable HTML.
Follow-ups
- How would you add a progress bar for file uploads?
- What happens to an in-flight request when the user navigates away?
- Why are requests with an Authorization header excluded from the transfer cache by default?
Tips
- Use the skip-auth example for HttpContext. It is concrete and short.
- Know the `fetch` limitation (upload progress).
- Mention the security side of the transfer cache. Senior reviewers care about data leaks.
Evaluation rubric
- Uses HttpContext correctly in an interceptor.
- Understands the fetch backend and its limits.
- Understands the transfer cache and its risks.