Skip to content

Commit ea81671

Browse files
Update docs to remove references to deprecated 'asp-prerender-webpack-config'. Fixes aspnet#359
1 parent b3dbb6e commit ea81671

File tree

1 file changed

+34
-51
lines changed
  • src/Microsoft.AspNetCore.SpaServices

1 file changed

+34
-51
lines changed

src/Microsoft.AspNetCore.SpaServices/README.md

Lines changed: 34 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Make sure you've installed the `Microsoft.AspNetCore.SpaServices` NuGet package
5252

5353
Now go to your `Views/_ViewImports.cshtml` file, and add the following line:
5454

55-
@addTagHelper "*, Microsoft.AspNetCore.SpaServices"
55+
@addTagHelper *, Microsoft.AspNetCore.SpaServices
5656

5757
### 2. Use asp-prerender-* in a view
5858

@@ -131,45 +131,54 @@ This can be useful if, for example, you want to avoid loading the same data twic
131131

132132
### 4. Enabling webpack build tooling
133133

134-
Of course, rather than writing your `boot-server` module and your entire SPA in plain ES5 JavaScript, it's quite likely that you'll want to write your client-side code in TypeScript or at least ES2015 code. To enable this, you can either:
134+
Of course, rather than writing your `boot-server` module and your entire SPA in plain ES5 JavaScript, it's quite likely that you'll want to write your client-side code in TypeScript or at least ES2015 code. To enable this, you need to set up a build system.
135135

136-
* Set up some build tool such as Babel to transpile to ES5, and always remember to run this to generate plain ES5 `.js` files before you run your application
137-
* Or, more conveniently, use [webpack](https://webpack.github.io/) along with the `asp-prerender-webpack-config` attribute so that `Microsoft.AspNetCore.SpaServices` can automatically build your boot module and the SPA code that it references. Then there's no need for `.js` files even to be written to disk - the build process is all dynamic and in memory.
136+
#### Example: Configuring Webpack to build TypeScript
138137

139-
To enable webpack builds for your server-side prerendering, amend your MVC view to specify the location of your webpack configuration file using an `asp-prerender-webpack-config` attribute, e.g.:
138+
Let's say you want to write your boot module and SPA code in TypeScript, and build it using Webpack. First ensure that `webpack` is installed, along with the libraries needed for TypeScript compilation:
140139

141-
<div id="my-spa" asp-prerender-module="ClientApp/boot-server"
142-
asp-prerender-webpack-config="webpack.config.js"></div>
143-
144-
You'll also need to install the NPM module `aspnet-webpack` if you don't have it already, e.g.:
145-
146-
npm install --save aspnet-webpack
147-
148-
This includes webpack as well as the server-side code needed to invoke it from ASP.NET Core at runtime.
149-
150-
Now, assuming you have a working webpack configuration at `webpack.config.js`, your boot module and SPA code will dynamically be built using webpack.
151-
152-
#### Example: Configuring webpack to build TypeScript
153-
154-
Let's say you want to write your boot module and SPA code in TypeScript. First ensure that `aspnet-webpack` is installed, along with the libraries needed for TypeScript compilation:
155-
156-
npm install --save aspnet-webpack ts-loader typescript
140+
npm install -g webpack
141+
npm install --save ts-loader typescript
157142

158143
Next, create a file `webpack.config.js` at the root of your project, containing:
159144

160145
```javascript
146+
var path = require('path');
147+
161148
module.exports = {
149+
entry: { 'main-server': './ClientApp/boot-server.ts' },
162150
resolve: { extensions: [ '', '.js', '.ts' ] },
151+
output: {
152+
path: path.join(__dirname, './ClientApp/dist'),
153+
filename: '[name].js',
154+
libraryTarget: 'commonjs'
155+
},
163156
module: {
164157
loaders: [
165158
{ test: /\.ts$/, loader: 'ts-loader' }
166159
]
167-
}
160+
},
161+
target: 'node',
162+
devtool: 'inline-source-map'
168163
};
169164
```
170165

171166
This tells webpack that it should compile `.ts` files using TypeScript, and that when looking for modules by name (e.g., `boot-server`), it should also find files with `.js` and `.ts` extensions.
172167

168+
If you don't already have a `tsconfig.json` file at the root of your project, add one now. Make sure your `tsconfig.json` includes `"es6"` in its `"lib"` array so that TypeScript knows about intrinsics such as `Promise`. Here's an example `tsconfig.json`:
169+
170+
```json
171+
{
172+
"compilerOptions": {
173+
"moduleResolution": "node",
174+
"target": "es5",
175+
"sourceMap": true,
176+
"lib": [ "es6", "dom" ]
177+
},
178+
"exclude": [ "bin", "node_modules" ]
179+
}
180+
```
181+
173182
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:
174183

175184
```javascript
@@ -186,12 +195,9 @@ export default function (params: any): Promise<{ html: string}> {
186195
}
187196
```
188197

189-
Finally, you can tell `SpaServices` to use the Webpack environment you've just set up. In your MVC view where you use `aspnet-prerender-module`, also specify `aspnet-prerender-webpack-config`:
190-
191-
<div id="my-spa" asp-prerender-module="ClientApp/boot-server"
192-
asp-prerender-webpack-config="webpack.config.js"></div>
198+
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:
193199

194-
Now your `boot-server.ts` code should get executed when your ASP.NET Core page is rendered, and since it's TypeScript, it can of course reference any other TypeScript modules, which means your entire SPA can be written in TypeScript and executed on the server.
200+
<div id="my-spa" asp-prerender-module="ClientApp/dist/main-server"></div>
195201

196202
Webpack is a broad and powerful tool and can do far more than just invoke the TypeScript compiler. To learn more, see the [webpack website](https://webpack.github.io/).
197203

@@ -278,30 +284,7 @@ You can now run your React code on the client by adding the following to one of
278284
<div id="my-spa"></div>
279285
<script src="/dist/main.js"></script>
280286

281-
#### Running React code on the server
282-
283-
Now you have React code being built using Webpack, you can enable server-side prerendering using the `aspnet-prerender-*` tag helpers as follows:
284-
285-
<div id="my-spa" asp-prerender-module="ClientApp/boot-server"
286-
asp-prerender-webpack-config="webpack.config.js"></div>
287-
288-
... along with the following boot module at `ClientApp/boot-server.jsx`:
289-
290-
```javascript
291-
import * as React from 'react';
292-
import { renderToString } from 'react-dom/server';
293-
import { HelloMessage } from './react-app';
294-
295-
export default function (params) {
296-
return new Promise((resolve, reject) => {
297-
resolve({
298-
html: renderToString(<HelloMessage message="from the server" />)
299-
});
300-
});
301-
}
302-
```
303-
304-
Now you should find that your React app is rendered in the page even before any JavaScript is loaded in the browser (or even if JavaScript is disabled in the browser).
287+
If you want to enable server-side prerendering too, follow the same process as described under [server-side prerendering](#server-side-prerendering).
305288

306289
#### Realistic React apps and Redux
307290

0 commit comments

Comments
 (0)