Skip to content

Commit 4d85608

Browse files
committed
snapshot 20180326
1 parent 84330e1 commit 4d85608

File tree

5 files changed

+33
-16
lines changed

5 files changed

+33
-16
lines changed

ClientApp/app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import Vue from 'vue'
22
import App from './components/App.vue'
33
import store from './vuex/store.js'
44
import router from './router'
5-
import BootstrapVue from 'bootstrap-vue'
5+
//import BootstrapVue from 'bootstrap-vue'
66

7-
Vue.use(BootstrapVue)
7+
//Vue.use(BootstrapVue)
88

99
const app = new Vue({
1010
router,

ClientApp/client.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import {
99
router,
1010
store
1111
} from './app'
12-
12+
import Vue from 'vue'
13+
import BootstrapVue from 'bootstrap-vue'
14+
Vue.use(BootstrapVue)
1315
store.replaceState(__INITIAL_STATE__)
1416

1517
router.onReady(() => {

ClientApp/renderOnServer.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,33 @@ const path = require('path')
66
const filePath = path.join(__dirname, '../wwwroot/dist/main-server.js')
77
const code = fs.readFileSync(filePath, 'utf8')
88

9-
var prerendering = require('aspnet-prerendering')
9+
const vue_renderer = require('vue-server-renderer').createBundleRenderer(code)
1010

1111
//prevent XSS attack when initialize state
1212
var serialize = require('serialize-javascript')
13-
13+
var prerendering = require('aspnet-prerendering')
1414
module.exports = prerendering.createServerRenderer(function (params) {
1515
return new Promise(
16-
function (resolve, reject) {
16+
function (resolve, reject) {
17+
// console.log('#prerendering#',params)
1718
const context = {
1819
url: params.url,
20+
absoluteUrl: params.absoluteUrl,
21+
baseUrl: params.baseUrl,
22+
data: params.data,
23+
domainTasks: params.domainTasks,
24+
location: params.location,
25+
origin: params.origin,
1926
xss: serialize("</script><script>alert('Possible XSS vulnerability from user input!')</script>")
2027
}
21-
resolve({
22-
globals: {
23-
__INITIAL_STATE__: context
24-
}
28+
const serverVueAppHtml = vue_renderer.renderToString(context, (err, _html) => {
29+
if(err) { reject(err.message) }
30+
resolve({
31+
globals: {
32+
html: _html,
33+
__INITIAL_STATE__: context.state
34+
}
35+
})
2536
})
2637
})
2738
});

ClientApp/server.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ export default context => {
1111
}
1212
Promise.all(matchedComponents.map(Component => {
1313
if (Component.asyncData) {
14-
return Component.asyncData({
15-
store
16-
})
14+
return Component.asyncData({ store })
1715
}
18-
})).then(() => {
16+
}))
17+
.then(() => {
1918
context.state = store.state
2019
resolve(app)
21-
}).catch(reject)
20+
})
21+
.catch(reject)
2222
}, reject)
2323
})
2424
}

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ To simulate timely API call form remote server, we add the following line in Hom
9999
- devDependencies:
100100
- aspnet-webpack
101101
- webpack-merge
102-
- dependencies:
102+
- dependencies:x
103103
- vue-server-renderer
104104
- aspnet-prerenderer
105105

@@ -181,3 +181,7 @@ and when you inspect the HTML from browser you will see the tags are escaped:
181181
window.__INITIAL_STATE__ = {"url":"/","xss":"\"\\u003C\\u002Fscript\\u003E\\u003Cscript\\u003Ealert('Possible XSS vulnerability from user input!')\\u003C\\u002Fscript\\u003E\""};
182182

183183
Cheers. :smirk:
184+
185+
### Reference
186+
*[Building Single Page Applications on ASP.NET Core with JavaScriptServices](https://blogs.msdn.microsoft.com/webdev/2017/02/14/building-single-page-applications-on-asp-net-core-with-javascriptservices/)*
187+
*[Use JavaScriptServices to Create Single Page Applications in ASP.NET Core](https://docs.microsoft.com/en-us/aspnet/core/client-side/spa-services)*

0 commit comments

Comments
 (0)