Skip to content

Commit e701449

Browse files
committed
add some comments
1 parent d7581f7 commit e701449

File tree

5 files changed

+34
-6
lines changed

5 files changed

+34
-6
lines changed

server.js

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const resolve = file => path.resolve(__dirname, file)
77
const express = require('express')
88
const favicon = require('serve-favicon')
99
const serialize = require('serialize-javascript')
10+
11+
// https://github.com/vuejs/vue/blob/next/packages/vue-server-renderer/README.md#why-use-bundlerenderer
1012
const createBundleRenderer = require('vue-server-renderer').createBundleRenderer
1113

1214
const app = express()

src/app.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,25 @@ import router from './router'
55
import { sync } from 'vuex-router-sync'
66
import * as filters from './filters'
77

8+
// sync the router with the vuex store.
9+
// this registers `store.state.route`
810
sync(store, router)
911

12+
// register global utility filters.
1013
Object.keys(filters).forEach(key => {
1114
Vue.filter(key, filters[key])
1215
})
1316

17+
// create the app instance.
18+
// here we inject the router and store to all child components,
19+
// making them available everywhere as `this.$router` and `this.$store`.
1420
const app = new Vue({
1521
router,
1622
store,
17-
...App
23+
...App // Object spread copying everything from App.vue
1824
})
1925

26+
// expose the app, the router and the store.
27+
// note we not mounting the app here, since bootstrapping will be
28+
// different depending on whether we are in browser or on the server.
2029
export { app, router, store }

src/client-entry.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
require('es6-promise').polyfill()
22
import { app, store } from './app'
33

4-
// prime the store with server-initialized state
4+
// prime the store with server-initialized state.
5+
// the state is determined during SSR and inlined in the page markup.
56
store.replaceState(window.__INITIAL_STATE__)
67

8+
// actually mount to DOM
79
app.$mount('#app')

src/server-entry.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,33 @@ import { app, router, store } from './app'
22

33
const isDev = process.env.NODE_ENV !== 'production'
44

5+
// This exported function will be called by `bundleRenderer`.
6+
// This is where we perform data-prefetching to determine the
7+
// state of our application before actually rendering it.
8+
// Since data fetching is async, this function is expected to
9+
// return a Promise that resolves to the app instance.
510
export default context => {
611
// set router's location
712
router.push(context.url)
8-
// call prefetch hooks on components matched by the route
13+
914
const s = isDev && Date.now()
15+
16+
// Call preFetch hooks on components matched by the route.
17+
// A preFetch hook dispatches a store action and returns a Promise,
18+
// which is resolved when the action is complete and store state has been
19+
// updated.
1020
return Promise.all(router.getMatchedComponents().map(component => {
1121
if (component.preFetch) {
1222
return component.preFetch(store)
1323
}
1424
})).then(() => {
1525
isDev && console.log(`data pre-fetch: ${Date.now() - s}ms`)
16-
// set initial store on context
17-
// the request handler will inline the state in the HTML response.
26+
// After all preFetch hooks are resolved, our store is now
27+
// filled with the needed state to render the app.
28+
// Expose the state on the render context, and let the request handler
29+
// inline the state in the HTML response. This allows the client-side
30+
// store to pick-up the server-side state without having to duplicate
31+
// the initial data fetching on the client.
1832
context.initialState = store.state
1933
return app
2034
})

src/views/CreateListView.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import ItemList from '../components/ItemList.vue'
22

3-
// factory function for creating root-level list views
3+
// This is a factory function for dynamically creating root-level list views,
44
// since they share most of the logic except for the type of items to display.
5+
// They are essentially higher order components wrapping ItemList.vue.
56
export function createListView (type) {
67
return {
78
name: `${type}-stories-view`,

0 commit comments

Comments
 (0)