From abd7f62b20156217c009b1a0b8031f17960490ee Mon Sep 17 00:00:00 2001 From: Mononaut Date: Sun, 29 Oct 2023 01:21:07 +0000 Subject: [PATCH] SSR: preserve transferstate api response headers --- .../src/app/dashboard/dashboard.component.ts | 1 - .../app/services/http-cache.interceptor.ts | 26 ++++++++++++------- .../src/app/services/websocket.service.ts | 2 +- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/frontend/src/app/dashboard/dashboard.component.ts b/frontend/src/app/dashboard/dashboard.component.ts index e54527939..98b89806c 100644 --- a/frontend/src/app/dashboard/dashboard.component.ts +++ b/frontend/src/app/dashboard/dashboard.component.ts @@ -8,7 +8,6 @@ import { StateService } from '../services/state.service'; import { WebsocketService } from '../services/websocket.service'; import { SeoService } from '../services/seo.service'; import { ActiveFilter, FilterMode, toFlags } from '../shared/filters.utils'; -import { TransferState } from '@angular/core'; interface MempoolBlocksData { blocks: number; diff --git a/frontend/src/app/services/http-cache.interceptor.ts b/frontend/src/app/services/http-cache.interceptor.ts index 289919534..c7624887a 100644 --- a/frontend/src/app/services/http-cache.interceptor.ts +++ b/frontend/src/app/services/http-cache.interceptor.ts @@ -1,5 +1,5 @@ import { Inject, Injectable, PLATFORM_ID } from '@angular/core'; -import { HttpInterceptor, HttpEvent, HttpRequest, HttpHandler, HttpResponse } from '@angular/common/http'; +import { HttpInterceptor, HttpEvent, HttpRequest, HttpHandler, HttpResponse, HttpHeaders } from '@angular/common/http'; import { Observable, of } from 'rxjs'; import { tap } from 'rxjs/operators'; import { TransferState, makeStateKey } from '@angular/platform-browser'; @@ -17,14 +17,18 @@ export class HttpCacheInterceptor implements HttpInterceptor { intercept(request: HttpRequest, next: HttpHandler): Observable> { if (this.isBrowser && request.method === 'GET') { - const cachedResponse = this.transferState.get(makeStateKey(request.url), null); - if (cachedResponse) { + const { response, headers } = this.transferState.get(makeStateKey(request.url), null) || {}; + if (response) { + const httpHeaders = new HttpHeaders(); + for (const [k,v] of Object.entries(headers)) { + httpHeaders.set(k,v as string[]); + } const modifiedResponse = new HttpResponse({ - headers: cachedResponse.headers, - body: cachedResponse.body, - status: cachedResponse.status, - statusText: cachedResponse.statusText, - url: cachedResponse.url + headers: httpHeaders, + body: response.body, + status: response.status, + statusText: response.statusText, + url: response.url }); this.transferState.remove(makeStateKey(request.url)); return of(modifiedResponse); @@ -35,7 +39,11 @@ export class HttpCacheInterceptor implements HttpInterceptor { .pipe(tap((event: HttpEvent) => { if (!this.isBrowser && event instanceof HttpResponse) { let keyId = request.url.split('/').slice(3).join('/'); - this.transferState.set(makeStateKey('/' + keyId), event); + const headers = {}; + for (const k of event.headers.keys()) { + headers[k] = event.headers.getAll(k); + } + this.transferState.set(makeStateKey('/' + keyId), { response: event, headers }); } })); } diff --git a/frontend/src/app/services/websocket.service.ts b/frontend/src/app/services/websocket.service.ts index 2256d174d..5ef637332 100644 --- a/frontend/src/app/services/websocket.service.ts +++ b/frontend/src/app/services/websocket.service.ts @@ -57,7 +57,7 @@ export class WebsocketService { this.network = this.stateService.network === 'bisq' && !this.stateService.env.BISQ_SEPARATE_BACKEND ? '' : this.stateService.network; this.websocketSubject = webSocket(this.webSocketUrl.replace('{network}', this.network ? '/' + this.network : '')); - const theInitData = this.transferState.get(initData, null); + const { response: theInitData } = this.transferState.get(initData, null) || {}; if (theInitData) { this.stateService.isLoadingWebSocket$.next(false); this.handleResponse(theInitData.body);