正如Vue官方所說,SSR配置適合已經熟悉 Vue, webpack 和 Node.js 開發的開發者閱讀。請先移步 ssr.vuejs.org 瞭解手工進行SSR配置的基本內容。css
從頭搭建一個服務端渲染的應用是至關複雜的。若是您有SSR需求,對Webpack及Koa不是很熟悉,請直接使用NUXT.js。html
本文所述內容示例在 Vue SSR Koa2 腳手架
:github.com/yi-ge/Vue-S…前端
咱們以撰寫本文時的最新版:Vue 2,Webpack 4,Koa 2爲例。vue
特別說明
此文描述的是API與WEB同在一個項目的狀況下進行的配置,且API、SSR Server、Static均使用了同一個Koa示例,目的是闡述配置方法,全部的報錯顯示在一個終端,方便調試。node
git init
yarn init
touch .gitignore
複製代碼
在.gitignore
文件,將常見的目錄放於其中。webpack
.DS_Store
node_modules
# 編譯後的文件如下兩個目錄
/dist/web
/dist/api
# Log files
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw*
複製代碼
根據經驗來預先添加確定會用到的依賴項:ios
echo "yarn add cross-env # 跨平臺的環境變量設置工具 koa koa-body # 可選,推薦 koa-compress # 壓縮數據 compressible # https://github.com/jshttp/compressible axios # 此項目做爲API請求工具 es6-promise vue vue-router # vue 路由 注意,SSR必選 vuex # 可選,但推薦使用,本文基於此作Vuex在SSR的優化 vue-template-compiler vue-server-renderer # 關鍵 lru-cache # 配合上面一個插件緩存數據 vuex-router-sync" | sed 's/#[[:space:]].*//g' | tr '\n' ' ' | sed 's/[ ][ ]*/ /g' | bash
echo "yarn add -D webpack webpack-cli webpack-dev-middleware # 關鍵 webpack-hot-middleware # 關鍵 webpack-merge # 合併多個Webpack配置文件的配置 webpack-node-externals # 不打包node_modules裏面的模塊 friendly-errors-webpack-plugin # 顯示友好的錯誤提示插件 case-sensitive-paths-webpack-plugin # 無視路徑大小寫插件 copy-webpack-plugin # 用於拷貝文件的Webpack插件 mini-css-extract-plugin # CSS壓縮插件 chalk # console着色 @babel/core # 不解釋 babel-loader @babel/plugin-syntax-dynamic-import # 支持動態import @babel/plugin-syntax-jsx # 兼容JSX寫法 babel-plugin-syntax-jsx # 不重複,必須的 babel-plugin-transform-vue-jsx babel-helper-vue-jsx-merge-props @babel/polyfill @babel/preset-env file-loader json-loader url-loader css-loader vue-loader vue-style-loader vue-html-loader" | sed 's/#[[:space:]].*//g' | tr '\n' ' ' | sed 's/[ ][ ]*/ /g' | bash
複製代碼
如今的npm模塊命名愈來愈語義化,基本上都是見名知意。關於Eslint以及Stylus、Less等CSS預處理模塊我沒有添加,其不是本文研究的重點,何況既然您在閱讀本文,這些配置相信早已不在話下了。git
效仿electorn
分離main及renderer,在src
中建立api
及web
目錄。效仿vue-cli
,在根目錄下建立public
目錄用於存放根目錄下的靜態資源文件。es6
|-- public # 靜態資源
|-- src
|-- api # 後端代碼
|-- web # 前端代碼
複製代碼
譬如NUXT.js
,前端服務器代理API進行後端渲染,咱們的配置能夠選擇進行一層代理,也能夠配置減小這層代理,直接返回渲染結果。一般來講,SSR的服務器端渲染只渲染首屏,所以API服務器最好和前端服務器在同一個內網。github
配置package.json
的scripts
:
"scripts": {
"serve": "cross-env NODE_ENV=development node config/server.js",
"start": "cross-env NODE_ENV=production node config/server.js"
}
複製代碼
yarn serve
: 啓動開發調試
yarn start
: 運行編譯後的程序
config/app.js
導出一些常見配置:
module.exports = {
app: {
port: 3000, // 監聽的端口
devHost: 'localhost', // 開發環境下打開的地址,監聽了0.0.0.0,可是不是全部設備都支持訪問這個地址,用127.0.0.1或localhost代替
open: true // 是否打開瀏覽器
}
}
複製代碼
咱們以Koa做爲調試和實際運行的服務器框架,config/server.js
:
const path = require('path')
const Koa = req uire('koa')
const koaCompress = require('koa-compress')
const compressible = require('compressible')
const koaStatic = require('./koa/static')
const SSR = require('./ssr')
const conf = require('./app')
const isProd = process.env.NODE_ENV === 'production'
const app = new Koa()
app.use(koaCompress({ // 壓縮數據
filter: type => !(/event\-stream/i.test(type)) && compressible(type) // eslint-disable-line
}))
app.use(koaStatic(isProd ? path.resolve(__dirname, '../dist/web') : path.resolve(__dirname, '../public'), {
maxAge: 30 * 24 * 60 * 60 * 1000
})) // 配置靜態資源目錄及過時時間
// vue ssr處理,在SSR中處理API
SSR(app).then(server => {
server.listen(conf.app.port, '0.0.0.0', () => {
console.log(`> server is staring...`)
})
})
複製代碼
上述文件咱們根據是不是開發環境,配置了對應的靜態資源目錄。須要說明的是,咱們約定編譯後的API文件位於dist/api
,前端文件位於dist/web
。
參考koa-static
實現靜態資源的處理,config/koa/static.js
:
'use strict'
/** * From koa-static */
const { resolve } = require('path')
const assert = require('assert')
const send = require('koa-send')
/** * Expose `serve()`. */
module.exports = serve
/** * Serve static files from `root`. * * @param {String} root * @param {Object} [opts] * @return {Function} * @api public */
function serve (root, opts) {
opts = Object.assign({}, opts)
assert(root, 'root directory is required to serve files')
// options
opts.root = resolve(root)
if (opts.index !== false) opts.index = opts.index || 'index.html'
if (!opts.defer) {
return async function serve (ctx, next) {
let done = false
if (ctx.method === 'HEAD' || ctx.method === 'GET') {
if (ctx.path === '/' || ctx.path === '/index.html') { // exclude index.html file
await next()
return
}
try {
done = await send(ctx, ctx.path, opts)
} catch (err) {
if (err.status !== 404) {
throw err
}
}
}
if (!done) {
await next()
}
}
}
return async function serve (ctx, next) {
await next()
if (ctx.method !== 'HEAD' && ctx.method !== 'GET') return
// response is already handled
if (ctx.body != null || ctx.status !== 404) return // eslint-disable-line
try {
await send(ctx, ctx.path, opts)
} catch (err) {
if (err.status !== 404) {
throw err
}
}
}
}
複製代碼
咱們能夠看到,koa-static
僅僅是對koa-send
進行了簡單封裝(yarn add koa-send
)。接下來就是重頭戲SSR相關的配置了, config/ssr.js
:
const fs = require('fs')
const path = require('path')
const chalk = require('chalk')
const LRU = require('lru-cache')
const {
createBundleRenderer
} = require('vue-server-renderer')
const isProd = process.env.NODE_ENV === 'production'
const setUpDevServer = require('./setup-dev-server')
const HtmlMinifier = require('html-minifier').minify
const pathResolve = file => path.resolve(__dirname, file)
module.exports = app => {
return new Promise((resolve, reject) => {
const createRenderer = (bundle, options) => {
return createBundleRenderer(bundle, Object.assign(options, {
cache: LRU({
max: 1000,
maxAge: 1000 * 60 * 15
}),
basedir: pathResolve('../dist/web'),
runInNewContext: false
}))
}
let renderer = null
if (isProd) {
// prod mode
const template = HtmlMinifier(fs.readFileSync(pathResolve('../public/index.html'), 'utf-8'), {
collapseWhitespace: true,
removeAttributeQuotes: true,
removeComments: false
})
const bundle = require(pathResolve('../dist/web/vue-ssr-server-bundle.json'))
const clientManifest = require(pathResolve('../dist/web/vue-ssr-client-manifest.json'))
renderer = createRenderer(bundle, {
template,
clientManifest
})
} else {
// dev mode
setUpDevServer(app, (bundle, options, apiMain, apiOutDir) => {
try {
const API = eval(apiMain).default // eslint-disable-line
const server = API(app)
renderer = createRenderer(bundle, options)
resolve(server)
} catch (e) {
console.log(chalk.red('\nServer error'), e)
}
})
}
app.use(async (ctx, next) => {
if (!renderer) {
ctx.type = 'html'
ctx.body = 'waiting for compilation... refresh in a moment.'
next()
return
}
let status = 200
let html = null
const context = {
url: ctx.url,
title: 'OK'
}
if (/^\/api/.test(ctx.url)) { // 若是請求以/api開頭,則進入api部分進行處理。
next()
return
}
try {
status = 200
html = await renderer.renderToString(context)
} catch (e) {
if (e.message === '404') {
status = 404
html = '404 | Not Found'
} else {
status = 500
console.log(chalk.red('\nError: '), e.message)
html = '500 | Internal Server Error'
}
}
ctx.type = 'html'
ctx.status = status || ctx.status
ctx.body = html
next()
})
if (isProd) {
const API = require('../dist/api/api').default
const server = API(app)
resolve(server)
}
})
}
複製代碼
這裏新加入了html-minifier
模塊來壓縮生產環境的index.html
文件(yarn add html-minifier
)。其他配置和官方給出的差很少,再也不贅述。只不過Promise返回的是require('http').createServer(app.callback())
(詳見源碼)。這樣作的目的是爲了共用一個koa2實例。此外,這裏攔截了/api
開頭的請求,將請求交由API Server進行處理(因在同一個Koa2實例,這裏直接next()了)。在public
目錄下必須存在index.html
文件:
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<title>{{ title }}</title>
...
</head>
<body>
<!--vue-ssr-outlet-->
</body>
</html>
複製代碼
開發環境中,處理數據的核心在config/setup-dev-server.js
文件:
const fs = require('fs')
const path = require('path')
const chalk = require('chalk')
const MFS = require('memory-fs')
const webpack = require('webpack')
const chokidar = require('chokidar')
const apiConfig = require('./webpack.api.config')
const serverConfig = require('./webpack.server.config')
const webConfig = require('./webpack.web.config')
const webpackDevMiddleware = require('./koa/dev')
const webpackHotMiddleware = require('./koa/hot')
const readline = require('readline')
const conf = require('./app')
const {
hasProjectYarn,
openBrowser
} = require('./lib')
const readFile = (fs, file) => {
try {
return fs.readFileSync(path.join(webConfig.output.path, file), 'utf-8')
} catch (e) {}
}
module.exports = (app, cb) => {
let apiMain, bundle, template, clientManifest, serverTime, webTime, apiTime
const apiOutDir = apiConfig.output.path
let isFrist = true
const clearConsole = () => {
if (process.stdout.isTTY) {
// Fill screen with blank lines. Then move to 0 (beginning of visible part) and clear it
const blank = '\n'.repeat(process.stdout.rows)
console.log(blank)
readline.cursorTo(process.stdout, 0, 0)
readline.clearScreenDown(process.stdout)
}
}
const update = () => {
if (apiMain && bundle && template && clientManifest) {
if (isFrist) {
const url = 'http://' + conf.app.devHost + ':' + conf.app.port
console.log(chalk.bgGreen.black(' DONE ') + ' ' + chalk.green(`Compiled successfully in ${serverTime + webTime + apiTime}ms`))
console.log()
console.log(` App running at: ${chalk.cyan(url)}`)
console.log()
const buildCommand = hasProjectYarn(process.cwd()) ? `yarn build` : `npm run build`
console.log(` Note that the development build is not optimized.`)
console.log(` To create a production build, run ${chalk.cyan(buildCommand)}.`)
console.log()
if (conf.app.open) openBrowser(url)
isFrist = false
}
cb(bundle, {
template,
clientManifest
}, apiMain, apiOutDir)
}
}
// server for api
apiConfig.entry.app = ['webpack-hot-middleware/client?path=/__webpack_hmr&timeout=2000&reload=true', apiConfig.entry.app]
apiConfig.plugins.push(
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin()
)
const apiCompiler = webpack(apiConfig)
const apiMfs = new MFS()
apiCompiler.outputFileSystem = apiMfs
apiCompiler.watch({}, (err, stats) => {
if (err) throw err
stats = stats.toJson()
if (stats.errors.length) return
console.log('api-dev...')
apiMfs.readdir(path.join(__dirname, '../dist/api'), function (err, files) {
if (err) {
return console.error(err)
}
files.forEach(function (file) {
console.info(file)
})
})
apiMain = apiMfs.readFileSync(path.join(apiConfig.output.path, 'api.js'), 'utf-8')
update()
})
apiCompiler.plugin('done', stats => {
stats = stats.toJson()
stats.errors.forEach(err => console.error(err))
stats.warnings.forEach(err => console.warn(err))
if (stats.errors.length) return
apiTime = stats.time
// console.log('web-dev')
// update()
})
// web server for ssr
const serverCompiler = webpack(serverConfig)
const mfs = new MFS()
serverCompiler.outputFileSystem = mfs
serverCompiler.watch({}, (err, stats) => {
if (err) throw err
stats = stats.toJson()
if (stats.errors.length) return
// console.log('server-dev...')
bundle = JSON.parse(readFile(mfs, 'vue-ssr-server-bundle.json'))
update()
})
serverCompiler.plugin('done', stats => {
stats = stats.toJson()
stats.errors.forEach(err => console.error(err))
stats.warnings.forEach(err => console.warn(err))
if (stats.errors.length) return
serverTime = stats.time
})
// web
webConfig.entry.app = ['webpack-hot-middleware/client?path=/__webpack_hmr&timeout=2000&reload=true', webConfig.entry.app]
webConfig.output.filename = '[name].js'
webConfig.plugins.push(
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin()
)
const clientCompiler = webpack(webConfig)
const devMiddleware = webpackDevMiddleware(clientCompiler, {
// publicPath: webConfig.output.publicPath,
stats: { // or 'errors-only'
colors: true
},
reporter: (middlewareOptions, options) => {
const { log, state, stats } = options
if (state) {
const displayStats = (middlewareOptions.stats !== false)
if (displayStats) {
if (stats.hasErrors()) {
log.error(stats.toString(middlewareOptions.stats))
} else if (stats.hasWarnings()) {
log.warn(stats.toString(middlewareOptions.stats))
} else {
log.info(stats.toString(middlewareOptions.stats))
}
}
let message = 'Compiled successfully.'
if (stats.hasErrors()) {
message = 'Failed to compile.'
} else if (stats.hasWarnings()) {
message = 'Compiled with warnings.'
}
log.info(message)
clearConsole()
update()
} else {
log.info('Compiling...')
}
},
noInfo: true,
serverSideRender: false
})
app.use(devMiddleware)
const templatePath = path.resolve(__dirname, '../public/index.html')
// read template from disk and watch
template = fs.readFileSync(templatePath, 'utf-8')
chokidar.watch(templatePath).on('change', () => {
template = fs.readFileSync(templatePath, 'utf-8')
console.log('index.html template updated.')
update()
})
clientCompiler.plugin('done', stats => {
stats = stats.toJson()
stats.errors.forEach(err => console.error(err))
stats.warnings.forEach(err => console.warn(err))
if (stats.errors.length) return
clientManifest = JSON.parse(readFile(
devMiddleware.fileSystem,
'vue-ssr-client-manifest.json'
))
webTime = stats.time
})
app.use(webpackHotMiddleware(clientCompiler))
}
複製代碼
因爲篇幅限制,koa
及lib
目錄下的文件參考示例代碼。其中lib
下的文件均來自vue-cli
,主要用於判斷用戶是否使用yarn
以及在瀏覽器中打開URL。 這時,爲了適應上述功能的須要,需添加如下模塊(可選):
yarn add memory-fs chokidar readline
yarn add -D opn execa
複製代碼
經過閱讀config/setup-dev-server.js
文件內容,您將發現此處進行了三個webpack配置的處理。
Server for API // 用於處理`/api`開頭下的API接口,提供非首屏API接入的能力
Web server for SSR // 用於服務器端對API的代理請求,實現SSR
WEB // 進行常規靜態資源的處理
複製代碼
|-- config
|-- webpack.api.config.js // Server for API
|-- webpack.base.config.js // 基礎Webpack配置
|-- webpack.server.config.js // Web server for SSR
|-- webpack.web.config.js // 常規靜態資源
複製代碼
因爲Webpack的配置較常規Vue項目以及Node.js項目並無太大區別,再也不一一贅述,具體配置請翻閱源碼。
值得注意的是,咱們爲API和WEB指定了別名:
alias: {
'@': path.join(__dirname, '../src/web'),
'~': path.join(__dirname, '../src/api'),
'vue$': 'vue/dist/vue.esm.js'
},
複製代碼
此外,webpack.base.config.js
中設定編譯時拷貝public
目錄下的文件到dist/web
目錄時並不包含index.html
文件。
編譯腳本:
"scripts": {
...
"build": "rimraf dist && npm run build:web && npm run build:server && npm run build:api",
"build:web": "cross-env NODE_ENV=production webpack --config config/webpack.web.config.js --progress --hide-modules",
"build:server": "cross-env NODE_ENV=production webpack --config config/webpack.server.config.js --progress --hide-modules",
"build:api": "cross-env NODE_ENV=production webpack --config config/webpack.api.config.js --progress --hide-modules"
},
複製代碼
執行yarn build
進行編譯。編譯後的文件存於/dist
目錄下。正式環境請儘可能分離API及SSR Server。
執行yarn serve
(開發)或yarn start
(編譯後)命令,訪問http://localhost:3000
。
經過查看源文件能夠看到,首屏渲染結果是這樣的:
➜ ~ curl -s http://localhost:3000/ | grep Hello
<div id="app" data-server-rendered="true"><div>Hello World SSR</div></div>
複製代碼
至此,Vue SSR配置完成。
原創內容。文章來源:www.wyr.me/post/593