8000 Example of using asp-prerender-data to get server-supplied data to bo… · aspnet/JavaScriptServices@678e230 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Commit 678e230

Browse files
Example of using asp-prerender-data to get server-supplied data to both server and client JS
1 parent ad645cb commit 678e230

File tree

5 files changed

+17
-5
lines changed

5 files changed

+17
-5
lines changed

templates/AngularSpa/ClientApp/app/app.module.client.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import { sharedConfig } from './app.module.shared';
1414
...sharedConfig.imports
1515
],
1616
providers: [
17-
{ provide: 'ORIGIN_URL', useValue: location.origin }
17+
{ provide: 'ORIGIN_URL', useValue: location.origin },
18+
{ provide: 'PRERENDERING_DATA', useValue: (window as any).PRERENDERING_DATA }
1819
]
1920
})
2021
export class AppModule {

templates/AngularSpa/ClientApp/app/components/fetchdata/fetchdata.component.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<h1>Weather forecast</h1>
22

3+
<p>Prerendering data: {{ prerenderingDataString }}</p>
4+
35
<p>This component demonstrates fetching data from the server.</p>
46

57
<p *ngIf="!forecasts"><em>Loading...</em></p>

templates/AngularSpa/ClientApp/app/components/fetchdata/fetchdata.component.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ import { Http } from '@angular/http';
77
})
88
export class FetchDataComponent {
99
public forecasts: WeatherForecast[];
10+
public prerenderingDataString: string;
11+
12+
constructor(http: Http, @Inject('ORIGIN_URL') originUrl: string, @Inject('PRERENDERING_DATA') prerenderingData: any) {
13+
this.prerenderingDataString = JSON.stringify(prerenderingData);
1014

11-
constructor(http: Http, @Inject('ORIGIN_URL') originUrl: string) {
1215
http.get(originUrl + '/api/SampleData/WeatherForecasts').subscribe(result => {
1316
this.forecasts = result.json() as WeatherForecast[];
14 10000 17
});

templates/AngularSpa/ClientApp/boot-server.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ enableProdMode();
1111
export default createServerRenderer(params => {
1212
const providers = [
1313
{ provide: INITIAL_CONFIG, useValue: { document: '<app></app>', url: params.url } },
14-
{ provide: 'ORIGIN_URL', useValue: params.origin }
14+
{ provide: 'ORIGIN_URL', useValue: params.origin },
15+
{ provide: 'PRERENDERING_DATA', useValue: params.data }
1516
];
1617

1718
return platformDynamicServer(providers).bootstrapModule(AppModule).then(moduleRef => {
@@ -26,7 +27,8 @@ export default createServerRenderer(params => {
2627
// completing the request in case there's an error to report
2728
setImmediate(() => {
2829
resolve({
29-
html: state.renderToString()
30+
html: state.renderToString(),
31+
globals: { PRERENDERING_DATA: params.data }
3032
});
3133
moduleRef.destroy();
3234
});

templates/AngularSpa/Views/Home/Index.cshtml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
ViewData["Title"] = "Home Page";
33
}
44

5-
<app asp-prerender-module="ClientApp/dist/main-server">Loading...</app>
5+
<app asp-prerender-module="ClientApp/dist/main-server"
6+
asp-prerender-data="new {
7+
IsGoldUser = true,
8+
Cookies = ViewContext.HttpContext.Request.Cookies
9+
}">Loading...</app>
610

711
<script src="~/dist/vendor.js" asp-append-version="true"></script>
812
@section scripts {

0 commit comments

Comments
 (0)
0