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.
from How to use HTTP Transfer State when using relative URLs
No comments:
Post a Comment