removed interceptor logic, enabling browser caching only
This commit is contained in:
@@ -11,7 +11,6 @@ import { MAT_FORM_FIELD_DEFAULT_OPTIONS } from '@angular/material/form-field';
|
||||
import { MatPaginatorIntl } from '@angular/material/paginator';
|
||||
import { PaginatorIntl } from './shared/utils/paginatorIntl';
|
||||
import { provideEchartsCore } from 'ngx-echarts';
|
||||
import { cachingInterceptor } from './shared/interceptors/caching.interceptor';
|
||||
|
||||
registerLocaleData(locale);
|
||||
|
||||
@@ -24,7 +23,7 @@ export const appConfig: ApplicationConfig = {
|
||||
},
|
||||
{ provide: MatPaginatorIntl, useClass: PaginatorIntl },
|
||||
provideRouter(routes),
|
||||
provideHttpClient(withInterceptors([authenticationInterceptor,cachingInterceptor])),
|
||||
provideHttpClient(withInterceptors([authenticationInterceptor])),
|
||||
// provideApi('/inse-service'),
|
||||
provideAnimationsAsync(),
|
||||
provideEchartsCore({ echarts: () => import('echarts') }),
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
import {
|
||||
HttpInterceptorFn,
|
||||
HttpResponse,
|
||||
HttpErrorResponse
|
||||
} from '@angular/common/http';
|
||||
import { catchError, of, tap, throwError } from 'rxjs';
|
||||
// here we do store our etags and responses from server.
|
||||
const etagStore = new Map<string, string>();
|
||||
const responseCache = new Map<string, any>();
|
||||
|
||||
export const cachingInterceptor: HttpInterceptorFn = (req, next) => {
|
||||
if (req.method !== 'GET') {
|
||||
return next(req);
|
||||
}
|
||||
// Do we have an ETag for the corresponding request?
|
||||
|
||||
const cacheKey = req.urlWithParams;
|
||||
const etag = etagStore.get(cacheKey);
|
||||
// If yes, set header to "If-None-Match" to let the server know we've got already an ETag.
|
||||
const modifiedReq = etag
|
||||
? req.clone({
|
||||
setHeaders: {
|
||||
'If-None-Match': etag
|
||||
}
|
||||
})
|
||||
: req;
|
||||
|
||||
// On server response:
|
||||
return next(modifiedReq).pipe(
|
||||
tap(event => {
|
||||
if (event instanceof HttpResponse) {
|
||||
const responseEtag = event.headers.get('ETag');
|
||||
// update the ETag and response body for our request
|
||||
console.log("FROM NEW BACKEND CALL , UPDATING ETAG",responseEtag)
|
||||
if (event.status === 200 && responseEtag) {
|
||||
etagStore.set(cacheKey, responseEtag);
|
||||
responseCache.set(cacheKey, event.body);
|
||||
}
|
||||
}
|
||||
}),
|
||||
// handle 304 separately (no data changes)
|
||||
catchError(err => {
|
||||
if ( // Server responded with 304 -> ETag hasnt changed -> use cached response body
|
||||
err instanceof HttpErrorResponse &&
|
||||
err.status === 304 &&
|
||||
responseCache.has(cacheKey)
|
||||
) {
|
||||
console.log("FROM CACHE!",cacheKey)
|
||||
return of(
|
||||
new HttpResponse({
|
||||
body: responseCache.get(cacheKey),
|
||||
status: 200,
|
||||
statusText: 'OK (cached)',
|
||||
url: req.urlWithParams
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
return throwError(() => err);
|
||||
})
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user