Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commiteb2c4c9

Browse files
committed
refactor(devtools): simular transfer cache data in the devtools demo app.
With the commit we mimic server-side behavior and serialize a bunch of transfer cache data.fixes#62664
1 parent527bd77 commiteb2c4c9

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

‎devtools/src/app/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ ng_project(
1212
srcs= [
1313
"app.component.ts",
1414
"app.config.ts",
15+
"transfer-state.ts",
1516
],
1617
angular_assets= [
1718
"app.component.html",

‎devtools/src/app/app.config.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import{ApplicationConfig}from'@angular/core';
9+
import{ApplicationConfig,provideAppInitializer}from'@angular/core';
1010
import{provideAnimations}from'@angular/platform-browser/animations';
1111
import{provideRouter}from'@angular/router';
1212
import{ApplicationEnvironment,ApplicationOperations}from'../../projects/ng-devtools';
1313

1414
import{DemoApplicationEnvironment}from'../demo-application-environment';
1515
import{DemoApplicationOperations}from'../demo-application-operations';
16+
import{serializeTransferState}from'./transfer-state';
17+
import{provideHttpClient,ɵwithHttpTransferCache}from'@angular/common/http';
1618

1719
exportconstappConfig:ApplicationConfig={
1820
providers:[
1921
provideAnimations(),
22+
provideHttpClient(),
2023
provideRouter([
2124
{
2225
path:'',
@@ -37,5 +40,9 @@ export const appConfig: ApplicationConfig = {
3740
provide:ApplicationEnvironment,
3841
useClass:DemoApplicationEnvironment,
3942
},
43+
// We simulate a transfer state created by the server-side rendering.
44+
// We use the privately exported interceptor to cache HTTP requests.
45+
ɵwithHttpTransferCache({}),
46+
provideAppInitializer(async()=>serializeTransferState()),
4047
],
4148
};

‎devtools/src/app/transfer-state.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import{HttpClient}from'@angular/common/http';
2+
import{APP_ID,DOCUMENT,inject,makeStateKey,TransferState}from'@angular/core';
3+
import{firstValueFrom}from'rxjs';
4+
5+
/**
6+
* This function serializes the transfer state into the DOM.
7+
*/
8+
exportasyncfunctionserializeTransferState():Promise<void>{
9+
constdoc=inject(DOCUMENT);
10+
constappId=inject(APP_ID);
11+
consttransferState=inject(TransferState);
12+
consthttpClient=inject(HttpClient);
13+
14+
// Object
15+
constObjkey=makeStateKey<any>('obj');
16+
transferState.set(Objkey,{
17+
appName:'DevTools',
18+
appVersion:'0.0.1',
19+
appDescription:'Angular DevTools',
20+
});
21+
22+
// Array
23+
constarraykey=makeStateKey<any>('arr');
24+
transferState.set(arraykey,[1,2,3,4,5]);
25+
26+
// Cached request
27+
// We're tweaking the transferCache interceptor to believe we're server side
28+
globalThis.ngServerMode=true;
29+
awaitfirstValueFrom(httpClient.get('https://swapi.info/api/people/1'));
30+
globalThis.ngServerMode=false;
31+
// We're back to normal
32+
33+
// Serialized of the TransferState into the DOM.
34+
constcontent=transferState.toJson();
35+
constscript=doc.createElement('script');
36+
script.textContent=content;
37+
38+
script.id=appId+'-state';
39+
script.setAttribute('type','application/json');
40+
41+
doc.body.appendChild(script);
42+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp