const koa = require('koa')
const Router = require('koa-router')
const axios = require('axios')
const path = require('path')
const fs = require('fs')
const MemoryFS = require('memory-fs')
const webpack = require('webpack')
const VueServerRenderer = require('vue-server-renderer')
const serverRender = require('./server-render')
const serverConfig = require('../../build/webpack.server.conf')
const serverCompiler = webpack(serverConfig)
const mfs = new MemoryFS()
serverCompiler.outputFileSystem = mfs
let bundle
serverCompiler.watch({}, (err, stats) => {
if (err) throw err
stats = stats.toJson()
stats.errors.forEach(err => console.log(err))
stats.warnings.forEach(warn => console.warn(warn))
const bundlePath = path.join(
serverConfig.output.path,
'vue-ssr-server-bundle.json'
)
bundle = JSON.parse(mfs.readFileSync(bundlePath, 'utf-8'))
console.log('new bundle generated')
console.log(bundle) **//available**
})
console.log(bundle) **//undefined**
const handleSSR = async (ctx) => {
if (bundle) {
ctx.body = '你等一会,别着急......'
return
}
const clientManifestResp = await axios.get(
'http://127.0.0.1:7777/vue-ssr-client-manifest.json'
)
const clientManifest = clientManifestResp.data
const template = fs.readFileSync(
path.join(__dirname, '../server.template.ejs'),
'utf-8'
)
const renderer = VueServerRenderer
.createBundleRenderer(bundle, {
inject: false,
clientManifest
})
await serverRender(ctx, renderer, template)
}
const pageRouter = new Router()
pageRouter.get('*', handleSSR)
module.exports = pageRouter
代码如上 TOP
问题描述:
1.两个bundle 的打印,一个是有效的,一个是无效的
2.我故意在里面写成, if(bundle),但是页面还是 not found,感觉没有执行handleSSR里面的内容
3.页面一直处于Not Found 的状态,学生内心是崩溃的~