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

Commit 2b2465a

Browse files
Update prerendering docs to account for aspnet-prerendering 2.0.0 and the new createServerRenderer API. Fixes #479
1 parent dc130ad commit 2b2465a

File tree

1 file changed

+18
-9
lines changed
  • src/Microsoft.AspNetCore.SpaServices

1 file changed

+18
-9
lines changed

src/Microsoft.AspNetCore.SpaServices/README.md

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,14 @@ Instead, what `SpaServices` offers is ASP.NET Core APIs that know how to invoke
4848

4949
### 1. Enable the asp-prerender-* tag helpers
5050

51-
Make sure you've installed the `Microsoft.AspNetCore.SpaServices` NuGet package and the `aspnet-prerendering` NPM package. Together these contain the server-side and client-side library code you'll need.
51+
Make sure you've installed into your project:
5252

53-
Now go to your `Views/_ViewImports.cshtml` file, and add the following line:
53+
* The `Microsoft.AspNetCore.SpaServices` NuGet package, version 1.1.0-* or later
54+
* The `aspnet-prerendering` NPM package, version 2.0.1 or later
5455

55-
@addTagHelper *, Microsoft.AspNetCore.SpaServices
56+
Together these contain the server-side and client-side library code you'll need. Now go to your `Views/_ViewImports.cshtml` file, and add the following line:
57+
58+
@addTagHelper "*, Microsoft.AspNetCore.SpaServices"
5659

5760
### 2. Use asp-prerender-* in a view
5861

@@ -67,7 +70,9 @@ If you run your application now, and browse to whatever page renders the view yo
6770
Create a JavaScript file at the path matching the `asp-prerender-module` value you specified above. In this example, that means creating a folder called `ClientApp` at the root of your project, and creating a file inside it called `boot-server.js`. Try putting the following into it:
6871

6972
```javascript
70-
module.exports = function(params) {
73+
var prerendering = require('aspnet-prerendering');
74+
75+
module.exports = prerendering.createServerRenderer(function(params) {
7176
return new Promise(function (resolve, reject) {
7277
var result = '<h1>Hello world!</h1>'
7378
+ '<p>Current time in Node is: ' + new Date() + '</p>'
@@ -76,7 +81,7 @@ module.exports = function(params) {
7681

7782
resolve({ html: result });
7883
});
79-
};
84+
});
8085
```
8186

8287
If you try running your app now, you should see the HTML snippet generated by your JavaScript getting injected into your page.
@@ -98,15 +103,17 @@ For example, in your `cshtml`,
98103
Now in your JavaScript prerendering function, you can access this data by reading `params.data`, e.g.:
99104

100105
```javascript
101-
module.exports = function(params) {
106+
var prerendering = require('aspnet-prerendering');
107+
108+
module.exports = prerendering.createServerRenderer(function(params) {
102109
return new Promise(function (resolve, reject) {
103110
var result = '<h1>Hello world!</h1>'
104111
+ '<p>Is gold user: ' + params.data.isGoldUser + '</p>'
105112
+ '<p>Number of cookies: ' + params.data.cookies.length + '</p>';
106113

107114
resolve({ html: result });
108115
});
109-
};
116+
});
110117
```
111118

112119
Notice that the property names are received in JavaScript-style casing (e.g., `isGoldUser`) even though they were sent in C#-style casing (e.g., `IsGoldUser`). This is because of how the JSON serialization is configured by default.
@@ -182,7 +189,9 @@ If you don't already have a `tsconfig.json` file at the root of your project, ad
182189
Now you can delete `ClientApp/boot-server.js`, and in its place, create `ClientApp/boot-server.ts`, containing the TypeScript equivalent of what you had before:
183190

184191
```javascript
185-
export default function (params: any): Promise<{ html: string}> {
192+
import { createServerRenderer } from 'aspnet-prerendering';
193+
194+
export default createServerRenderer(params => {
186195
return new Promise((resolve, reject) => {
187196
const html = `
188197
<h1>Hello world!</h1>
@@ -192,7 +201,7 @@ export default function (params: any): Promise<{ html: string}> {
192201

193202
resolve({ html });
194203
});
195-
}
204+
});
196205
```
197206

198207
Finally, run `webpack` on the command line to build `ClientApp/dist/main-server.js`. Then you can tell `SpaServices` to use that file for server-side prerendering. In your MVC view where you use `aspnet-prerender-module`, update the attribute value:

0 commit comments

Comments
 (0)