Browse Source

Etag support for the UI tool.

pull/330/head
Sebastian Stehle 7 years ago
parent
commit
6ba6477856
  1. 46
      src/Squidex/app/framework/angular/http/caching.interceptor.ts
  2. 1
      src/Squidex/app/framework/declarations.ts
  3. 6
      src/Squidex/app/framework/module.ts

46
src/Squidex/app/framework/angular/http/caching.interceptor.ts

@ -0,0 +1,46 @@
/*
* Squidex Headless CMS
*
* @license
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved.
*/
import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse } from '@angular/common/http';
import { Injectable} from '@angular/core';
import { Observable, of, throwError } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';
import { Types } from './../../internal';
@Injectable()
export class CachingInterceptor implements HttpInterceptor {
private readonly cache: { [url: string]: HttpResponse<any> } = {};
public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (req.method === 'GET' && req.reportProgress === false) {
const cacheEntry = this.cache[req.url];
if (cacheEntry) {
req = req.clone({ headers: req.headers.set('If-None-Match', cacheEntry.headers.get('Etag')!) });
}
return next.handle(req).pipe(
tap(response => {
if (Types.is(response, HttpResponse)) {
if (response.headers.get('Etag')) {
this.cache[req.url] = response;
}
}
}),
catchError(error => {
if (Types.is(error, HttpErrorResponse) && error.status === 304 && cacheEntry) {
return of(cacheEntry);
} else {
return throwError(error);
}
}));
} else {
return next.handle(req);
}
}
}

1
src/Squidex/app/framework/declarations.ts

@ -27,6 +27,7 @@ export * from './angular/forms/toggle.component';
export * from './angular/forms/transform-input.directive';
export * from './angular/forms/validators';
export * from './angular/http/caching.interceptor';
export * from './angular/http/loading.interceptor';
export * from './angular/http/http-extensions';

6
src/Squidex/app/framework/module.ts

@ -13,6 +13,7 @@ import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import {
AnalyticsService,
AutocompleteComponent,
CachingInterceptor,
CanDeactivateGuard,
ClipboardService,
ConfirmClickDirective,
@ -233,6 +234,11 @@ export class SqxFrameworkModule {
provide: HTTP_INTERCEPTORS,
useClass: LoadingInterceptor,
multi: true
},
{
provide: HTTP_INTERCEPTORS,
useClass: CachingInterceptor,
multi: true
}
]
};

Loading…
Cancel
Save