You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -131,45 +131,54 @@ This can be useful if, for example, you want to avoid loading the same data twic
131
131
132
132
### 4. Enabling webpack build tooling
133
133
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.
135
135
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
138
137
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:
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:
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.
172
167
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
+
173
182
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:
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`:
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:
193
199
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.
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/).
197
203
@@ -278,30 +284,7 @@ You can now run your React code on the client by adding the following to one of
278
284
<div id="my-spa"></div>
279
285
<script src="/dist/main.js"></script>
280
286
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:
... along with the following boot module at `ClientApp/boot-server.jsx`:
289
-
290
-
```javascript
291
-
import*asReactfrom'react';
292
-
import { renderToString } from'react-dom/server';
293
-
import { HelloMessage } from'./react-app';
294
-
295
-
exportdefaultfunction (params) {
296
-
returnnewPromise((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).
0 commit comments