|
1 |
| -This repository is a clone from Stu Ratcliffe [Server rendering Vue.js applications with ASP.NET Core](https://github.com/sturatcliffe/VueDotnetSSR) |
| 1 | +# README |
2 | 2 |
|
3 |
| -Following his articile and trying to have some write up. (e.g. using latest packages in package.json) |
| 3 | +This repository was built from scratch following the steps described in the original blog post *[Server rendering Vue.js applications with ASP.NET Core](https://stu.ratcliffe.io/2017/07/20/vuejs-serverside-rendering-with-aspnet-core)* from Stu Ratcliffe. The aim of this repository is to adding more steps and comments from my experience following the steps that make it works as a supplement of the original blog post. Hope this helps if you also read the same blog post and got stuck some way. :) |
| 4 | + |
| 5 | +You can get the complete code repo made by Stu Ratcliffe from [[Here]](https://github.com/sturatcliffe/VueDotnetSSR) |
4 | 6 |
|
5 | 7 | ## Install and run:
|
6 | 8 | if Webpack is not installed yet:
|
|
13 | 15 | webpack
|
14 | 16 | dotnet run
|
15 | 17 |
|
16 |
| -## Packages used: |
| 18 | +## Packages used: *The description may not accurate, just my understanding* |
| 19 | +- vue <- VueJS |
17 | 20 | - lodash <- similiar to numpy in Python, utilies library for manipulating array/object.
|
18 | 21 | - axios <- Promise based HTTP client for the browser and Node.js, think of $.ajax() if you come from jQuery world
|
19 |
| -- vuex <- Vue variant of Flux implementation, just like Redux in React |
| 22 | +- vuex <- Vue variant of Flux implementation, just like Redux in React |
| 23 | +- nprogress <- loading indiciator |
| 24 | +- vue-router <- enable client side "page" routing |
| 25 | +- vue-server-renderer <- enable Server Side Rendering |
| 26 | +- aspnet-prerendering <- enable ASP.NET to trigger Node for SSR |
| 27 | + |
| 28 | +- webpack <- pack your JavaScript files into bundles for faster loading, remove duplicate imports, reduce final code size |
| 29 | +- webpack-merge <- merge webpack config so common configuration attributes can be shared among configurations |
| 30 | +- webpack-hot-middleware <- enable hot reload of code changes |
| 31 | +- aspnet-webpack <- enable ASP.NET to execute webpack on demand during runtime. |
| 32 | +- vue-loader <- required for packing vuejs code |
| 33 | +- css-loader <- required if you have css file to pack |
| 34 | +- style-loader <- required if you use template with style |
| 35 | +- json-loader <- requiredd if you need to pack json file |
| 36 | +- vue-template-compiler <- required for packing if you use template in vue components |
| 37 | +- babel-* <- transpile code syntax used in Vue into browser understandable version before packing |
| 38 | + |
20 | 39 | ### Working with data:
|
21 | 40 | - .Net part
|
22 | 41 | 1. Message.cs
|
|
25 | 44 | - Vue part
|
26 | 45 | 0. (create ClientApp/vuex folder)
|
27 | 46 | 1. ClientApp/vuex/action.js
|
28 |
| - 2. ClientApp/vuex/store.js |
| 47 | + 2. ClientApp/vuex/store.js |
| 48 | + |
29 | 49 | ### Client side routing:
|
30 | 50 | 0. Move to ClientApp/components
|
31 | 51 | 1. Dashboard.vue
|
|
37 | 57 | 7. Move back to ClientApp folder
|
38 | 58 | 8. Modify app.js
|
39 | 59 | 9. Move back to .
|
40 |
| -10. Modify Startup.cs |
| 60 | +10. Modify Startup.cs |
| 61 | + |
41 | 62 | ### Loading indicator:
|
42 | 63 | Here we try to modify the implementation order different from the original post.
|
43 | 64 | We are going to add the loading indicator before implementing the Server Side Rendering.
|
@@ -67,4 +88,35 @@ To simulate timely API call form remote server, we add the following line in Hom
|
67 | 88 | loader: "style-loader!css-loader"
|
68 | 89 | }
|
69 | 90 |
|
70 |
| -3. Modify ClientApp/vuex/actions.js, add `NProgress.start()` and `NProgress.done()` before and after axios remote call. |
| 91 | +3. Modify `ClientApp/vuex/actions.js`, add `NProgress.start()` and `NProgress.done()` before and after axios remote call. |
| 92 | + |
| 93 | +### Server Side Renderering (SSR) |
| 94 | + |
| 95 | +1. Add the following dependencies to package.json: |
| 96 | + |
| 97 | + - devDependencies: |
| 98 | + - aspnet-webpack |
| 99 | + - webpack-merge |
| 100 | + - dependencies: |
| 101 | + - vue-server-renderer |
| 102 | + - aspnet-prerenderer *(Note that only version ^1.0.0 is supported, using latest ^3.0.0+ will break the code.)* |
| 103 | + |
| 104 | +2. Split the code into two part: |
| 105 | + |
| 106 | + 1. `server.js` <- this will load by renderOnServer.js, which aspnet-prerendering will trigger Node to execute and return pre-rendered result back to renderOnServer.js and thus send to browser as initial state of app. |
| 107 | + 2. `client.js` <- once initial app state rendered and injected in the resulting index.cshtml, the script tag will load client.js and mount it to pre-rendered app tag. |
| 108 | + |
| 109 | +3. Create Node server code for ASP.NET Core to trigger the Node hosting service to execute |
| 110 | + |
| 111 | + - `rendererOnServer.js` <- responsible for loading the webpacked server.js for Node to render the initial app state. |
| 112 | + |
| 113 | +4. ASP.NET Core part |
| 114 | + |
| 115 | + - Edit `Views/Home/index.cshtml` app tag to use `aspnet-prerendering` attributes |
| 116 | + |
| 117 | +5. Webpack Configuration |
| 118 | + |
| 119 | + - Edit `webpack.config.js`, make use of `webpack-merge` to split the original configuration into two sets. |
| 120 | + |
| 121 | +### Thoughts: |
| 122 | +SSR was by far the most difficult part of my VueJS journey, it takes more than half of the time of my VueJS learning. Whether to use Server Side Rendering or not is highly optional, you don't need it to write a cool SPA. The performance and user experience gain is arguablely worth the complexity and develop time involved. |
0 commit comments