I am trying to implement built-in TransferHttpCacheModule in order to de-duplicate requests. I am using this interceptor in my app:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const authService = this.injector.get(AuthenticationService);
const url = `${this.request ? this.request.protocol + '://' + this.request.get('host') : ''}${environment.baseBackendUrl}${req.url}`
let headers = new HttpHeaders();
if (this.request) {
// Server side: forward the cookies
const cookies = this.request.cookies;
const cookiesArray = [];
for (const name in cookies) {
if (cookies.hasOwnProperty(name)) {
cookiesArray.push(`${name}=${cookies[name]}`);
}
}
headers = headers.append('Cookie', cookiesArray.join('; '));
}
headers = headers.append('Content-Type', 'application/json');
const finalReq: HttpRequest<any> = req.clone({ url, headers });
...
It enables relative URLs for client side and full URLs for server side since the server is not aware of its own URL.
The problem is that TransferHttpCacheModule
uses a key based on the method, the URL and the parameters, and the server URLs don't match with the client URLs.
Is there any way to force the TransferHttpCacheInterceptor
to execute before my own interceptor? I want to avoid forcing full URLs on client side.
You can place your interceptor inside its own module:
You can then place this module below the import of the
TransferHttpCacheModule
inside your AppModule:This way your interceptor will be applied after the
TransferHttpCacheInterceptor
. It feels weird though, because as far as I know, an import is first in line, and then the providers. This way you can override providers from imports. Are you sure you don't want it the other way around?