This repository was archived by the owner on Nov 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 474
Expand file tree
/
Copy pathtransfer_http.ts
More file actions
158 lines (141 loc) · 4.36 KB
/
transfer_http.ts
File metadata and controls
158 lines (141 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {
HTTP_INTERCEPTORS,
HttpEvent,
HttpHandler,
HttpHeaders,
HttpInterceptor,
HttpParams,
HttpRequest,
HttpResponse,
} from '@angular/common/http';
import {
ApplicationRef,
Injectable,
NgModule,
StateKey,
TransferState,
makeStateKey,
} from '@angular/core';
import { Observable, of as observableOf } from 'rxjs';
import { defaultIfEmpty, first, tap } from 'rxjs/operators';
type ResponseType = HttpRequest<unknown>['responseType'];
interface TransferHttpResponse {
body: any;
headers: Record<string, string[]>;
status?: number;
statusText?: string;
url?: string;
responseType?: ResponseType;
}
function getHeadersMap(headers: HttpHeaders): Record<string, string[]> {
const headersMap: Record<string, string[]> = {};
for (const key of headers.keys()) {
const values = headers.getAll(key);
if (values !== null) {
headersMap[key] = values;
}
}
return headersMap;
}
/**
* @deprecated Use `provideClientHydration` instead which caches HTTP requests by default.
* @see https://angular.io/api/platform-browser/provideClientHydration
*/
@Injectable()
export class TransferHttpCacheInterceptor implements HttpInterceptor {
private isCacheActive = true;
private makeCacheKey(
method: string,
url: string,
params: HttpParams,
responseType: ResponseType,
): StateKey<TransferHttpResponse> {
// make the params encoded same as a url so it's easy to identify
const encodedParams = params
.keys()
.sort()
.map((k) => `${k}=${params.getAll(k)}`)
.join('&');
const key = (method === 'GET' ? 'G.' : 'H.') + responseType + '.' + url + '?' + encodedParams;
return makeStateKey<TransferHttpResponse>(key);
}
constructor(appRef: ApplicationRef, private transferState: TransferState) {
// Stop using the cache if the application has stabilized, indicating initial rendering is
// complete.
appRef.isStable
.pipe(
first((isStable) => isStable),
defaultIfEmpty(false),
)
.subscribe(() => {
this.isCacheActive = false;
});
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (!this.isCacheActive || (req.method !== 'GET' && req.method !== 'HEAD')) {
// Cache is no longer active or method is not HEAD or GET.
// Pass the request through.
return next.handle(req);
}
const storeKey = this.makeCacheKey(req.method, req.url, req.params, req.responseType);
if (this.transferState.hasKey(storeKey)) {
// Request found in cache. Respond using it.
const response = this.transferState.get(storeKey, null);
let body: ArrayBuffer | Blob | string | undefined = response?.body;
switch (response?.responseType) {
case 'arraybuffer':
body = new TextEncoder().encode(response.body).buffer;
break;
case 'blob':
body = new Blob([response.body]);
break;
}
return observableOf(
new HttpResponse<any>({
body,
headers: new HttpHeaders(response?.headers),
status: response?.status,
statusText: response?.statusText,
url: response?.url,
}),
);
} else {
// Request not found in cache. Make the request and cache it.
const httpEvent = next.handle(req);
return httpEvent.pipe(
tap((event: HttpEvent<unknown>) => {
if (event instanceof HttpResponse) {
this.transferState.set<TransferHttpResponse>(storeKey, {
body: event.body,
headers: getHeadersMap(event.headers),
status: event.status,
statusText: event.statusText,
url: event.url || '',
responseType: req.responseType,
});
}
}),
);
}
}
}
/**
* An NgModule used in conjunction with `ServerTransferHttpCacheModule` to transfer cached HTTP
* calls from the server to the client application.
*/
@NgModule({
providers: [
ApplicationRef,
TransferState,
TransferHttpCacheInterceptor,
{ provide: HTTP_INTERCEPTORS, useExisting: TransferHttpCacheInterceptor, multi: true },
],
})
export class TransferHttpCacheModule {}