I have a simple caching interceptor and I want that my cache is immutable. But somehow it's not working. The original code is from the HttpClient doc https://angular.io/guide/http#intercepting-all-requests-or-responses
export class HttpCachingInterceptor implements HttpInterceptor {
private cache = new Map<string, HttpResponse<any>>();
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// Before doing anything, it's important to only cache GET requests.
// Skip this interceptor if the request method isn't GET.
if (req.method !== 'GET') {
return next.handle(req);
}
// First, check the cache to see if this request exists.
const cachedResponse = this.cache.get(req.urlWithParams);
if (cachedResponse && !req.headers.get('disable-cache')) {
// A cached response exists. Serve it instead of forwarding
// the request to the next handler.
return Observable.of(cachedResponse.clone());
}
// No cached response exists. Go to the network, and cache
// the response when it arrives.
return next.handle(req).do(event => {
// Remember, there may be other events besides just the response.
if (event instanceof HttpResponse && !req.headers.get('disable-cache')) {
// Update the cache.
this.cache.set(req.urlWithParams, event.clone());
}
});
}
}
I have two components where data is accessed. First component retrieves the complete data without cache (first request) and modifies the data by
myHttpService.getCategories().map((root) => {
root.categories.splice(0, 1);
The second component uses the same service myHttpService but the response is served from the cache and the data is mutated. I know I could use slice in that case but my cache should be immutable how can I achieve that without using additional libraries?