Skip to content

Commit c255945

Browse files
committed
more tweaks
1 parent af46a71 commit c255945

File tree

4 files changed

+25
-25
lines changed

4 files changed

+25
-25
lines changed

server.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
process.env.VUE_ENV = 'server'
2+
const isProd = process.env.NODE_ENV === 'production'
23

34
const fs = require('fs')
45
const path = require('path')
6+
const resolve = file => path.resolve(__dirname, file)
57
const express = require('express')
68
const favicon = require('serve-favicon')
79
const serialize = require('serialize-javascript')
@@ -11,11 +13,10 @@ const app = express()
1113

1214
// parse index.html template
1315
const html = (() => {
14-
const template = fs.readFileSync(path.resolve(__dirname, './index.html'), 'utf-8')
16+
const template = fs.readFileSync(resolve('./index.html'), 'utf-8')
1517
const i = template.indexOf('{{ APP }}')
16-
const style = process.env.NODE_ENV === 'production'
17-
? '<link rel="stylesheet" href="/dist/styles.css">'
18-
: ''
18+
// styles are injected dynamically via vue-style-loader in development
19+
const style = isProd ? '<link rel="stylesheet" href="/dist/styles.css">' : ''
1920
return {
2021
head: template.slice(0, i).replace('{{ STYLE }}', style),
2122
tail: template.slice(i + '{{ APP }}'.length)
@@ -24,18 +25,18 @@ const html = (() => {
2425

2526
// setup the server renderer, depending on dev/prod environment
2627
let renderer
27-
if (process.env.NODE_ENV !== 'production') {
28+
if (isProd) {
29+
// create server renderer from real fs
30+
const bundlePath = resolve('./dist/server-bundle.js')
31+
renderer = createBundleRenderer(fs.readFileSync(bundlePath, 'utf-8'))
32+
} else {
2833
require('./build/setup-dev-server')(app, bundle => {
2934
renderer = createBundleRenderer(bundle)
3035
})
31-
} else {
32-
// create server renderer from real fs
33-
const bundlePath = path.resolve(__dirname, './dist/server-bundle.js')
34-
renderer = createBundleRenderer(fs.readFileSync(bundlePath, 'utf-8'))
3536
}
3637

37-
app.use('/dist', express.static(path.resolve(__dirname, './dist')))
38-
app.use(favicon(path.resolve(__dirname, './src/assets/logo.png')))
38+
app.use('/dist', express.static(resolve('./dist')))
39+
app.use(favicon(resolve('./src/assets/logo.png')))
3940

4041
app.get('*', (req, res) => {
4142
var s = Date.now()

src/client-entry.js

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

3-
import { app } from './app'
4+
// prime the store with server-initialized state
5+
store.replaceState(window.__INITIAL_STATE__)
46

57
app.$mount('#app')

src/server-entry.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import { app, router, store } from './app'
22

3+
const isDev = process.env.NODE_ENV !== 'production'
4+
35
export default context => {
46
// set router's location
57
router.push(context.url)
68
// call prefetch hooks on components matched by the route
7-
const s = Date.now()
9+
const s = isDev && Date.now()
810
return Promise.all(router.getMatchedComponents().map(component => {
911
if (component.prefetch) {
1012
return component.prefetch(store)
1113
}
1214
})).then(() => {
13-
console.log(`data pre-fetch: ${Date.now() - s}ms`)
15+
isDev && console.log(`data pre-fetch: ${Date.now() - s}ms`)
1416
// set initial store on context
1517
// the request handler will inline the state in the HTML response.
1618
context.initialState = store.state

src/store/index.js

+6-11
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@ import { watchTopIds, fetchTopIds, fetchItems } from './api'
44

55
Vue.use(Vuex)
66

7-
const inBrowser = typeof window !== 'undefined'
8-
9-
// if in browser, use pre-fetched state injected by SSR
10-
const state = (inBrowser && window.__INITIAL_STATE__) || {
11-
storiesPerPage: 20,
12-
topStoryIds: [],
13-
items: {}
14-
}
15-
167
const store = new Vuex.Store({
17-
state,
8+
state: {
9+
storiesPerPage: 20,
10+
topStoryIds: [],
11+
items: {}
12+
},
1813

1914
actions: {
2015
FETCH_TOP_IDS: ({ commit }) => {
@@ -50,7 +45,7 @@ const store = new Vuex.Store({
5045
})
5146

5247
// watch for realtime top IDs updates on the client
53-
if (inBrowser) {
48+
if (typeof window !== 'undefined') {
5449
watchTopIds(ids => {
5550
store.commit('RECEIVE_TOP_IDS', { ids })
5651
store.dispatch('FETCH_NEWS')

0 commit comments

Comments
 (0)