SSR: preserve transferstate api response headers
This commit is contained in:
		
							parent
							
								
									bcd337794a
								
							
						
					
					
						commit
						abd7f62b20
					
				| @ -8,7 +8,6 @@ import { StateService } from '../services/state.service'; | |||||||
| import { WebsocketService } from '../services/websocket.service'; | import { WebsocketService } from '../services/websocket.service'; | ||||||
| import { SeoService } from '../services/seo.service'; | import { SeoService } from '../services/seo.service'; | ||||||
| import { ActiveFilter, FilterMode, toFlags } from '../shared/filters.utils'; | import { ActiveFilter, FilterMode, toFlags } from '../shared/filters.utils'; | ||||||
| import { TransferState } from '@angular/core'; |  | ||||||
| 
 | 
 | ||||||
| interface MempoolBlocksData { | interface MempoolBlocksData { | ||||||
|   blocks: number; |   blocks: number; | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| import { Inject, Injectable, PLATFORM_ID } from '@angular/core'; | 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 { Observable, of } from 'rxjs'; | ||||||
| import { tap } from 'rxjs/operators'; | import { tap } from 'rxjs/operators'; | ||||||
| import { TransferState, makeStateKey } from '@angular/platform-browser'; | 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>> { |   intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | ||||||
|     if (this.isBrowser && request.method === 'GET') { |     if (this.isBrowser && request.method === 'GET') { | ||||||
| 
 | 
 | ||||||
|       const cachedResponse = this.transferState.get<any>(makeStateKey(request.url), null); |       const { response, headers } = this.transferState.get<any>(makeStateKey(request.url), null) || {}; | ||||||
|       if (cachedResponse) { |       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>({ |         const modifiedResponse = new HttpResponse<any>({ | ||||||
|           headers: cachedResponse.headers, |           headers: httpHeaders, | ||||||
|           body: cachedResponse.body, |           body: response.body, | ||||||
|           status: cachedResponse.status, |           status: response.status, | ||||||
|           statusText: cachedResponse.statusText, |           statusText: response.statusText, | ||||||
|           url: cachedResponse.url |           url: response.url | ||||||
|         }); |         }); | ||||||
|         this.transferState.remove(makeStateKey(request.url)); |         this.transferState.remove(makeStateKey(request.url)); | ||||||
|         return of(modifiedResponse); |         return of(modifiedResponse); | ||||||
| @ -35,7 +39,11 @@ export class HttpCacheInterceptor implements HttpInterceptor { | |||||||
|       .pipe(tap((event: HttpEvent<any>) => { |       .pipe(tap((event: HttpEvent<any>) => { | ||||||
|         if (!this.isBrowser && event instanceof HttpResponse) { |         if (!this.isBrowser && event instanceof HttpResponse) { | ||||||
|           let keyId = request.url.split('/').slice(3).join('/'); |           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.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 : '')); |       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) { |       if (theInitData) { | ||||||
|         this.stateService.isLoadingWebSocket$.next(false); |         this.stateService.isLoadingWebSocket$.next(false); | ||||||
|         this.handleResponse(theInitData.body); |         this.handleResponse(theInitData.body); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user