mirror of
https://github.com/mempool/mempool.git
synced 2025-02-24 22:58:30 +01:00
SSR: preserve transferstate api response headers
This commit is contained in:
parent
bcd337794a
commit
abd7f62b20
3 changed files with 18 additions and 11 deletions
|
@ -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;
|
||||
|
|
|
@ -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<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
||||
if (this.isBrowser && request.method === 'GET') {
|
||||
|
||||
const cachedResponse = this.transferState.get<any>(makeStateKey(request.url), null);
|
||||
if (cachedResponse) {
|
||||
const { response, headers } = this.transferState.get<any>(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<any>({
|
||||
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<any>) => {
|
||||
if (!this.isBrowser && event instanceof HttpResponse) {
|
||||
let keyId = request.url.split('/').slice(3).join('/');
|
||||
this.transferState.set<any>(makeStateKey('/' + keyId), event);
|
||||
const headers = {};
|
||||
for (const k of event.headers.keys()) {
|
||||
headers[k] = event.headers.getAll(k);
|
||||
}
|
||||
this.transferState.set<any>(makeStateKey('/' + keyId), { response: event, headers });
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
|
|
@ -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<WebsocketResponse>(this.webSocketUrl.replace('{network}', this.network ? '/' + this.network : ''));
|
||||
|
||||
const theInitData = this.transferState.get<any>(initData, null);
|
||||
const { response: theInitData } = this.transferState.get<any>(initData, null) || {};
|
||||
if (theInitData) {
|
||||
this.stateService.isLoadingWebSocket$.next(false);
|
||||
this.handleResponse(theInitData.body);
|
||||
|
|
Loading…
Add table
Reference in a new issue