vue: SSR fails when split code
Version
2.5.16
Reproduction link
https://github.com/vincent-yao27/vue-ssr-bug
Steps to reproduce
- Create a project with vue-cli 3.
- Set up SSR.
- Use
import('path/to/component.vue')to split code. - Build server bundle.
- Start a server to render the pages.
// vue.config.js
const SSRServerPlugin = require('vue-server-renderer/server-plugin')
const nodeExternals = require('webpack-node-externals')
module.exports = {
configureWebpack: {
entry: './src/entry-server',
target: 'node',
devtool: 'source-map',
output: {
libraryTarget: 'commonjs2'
},
externals: nodeExternals({ whitelist: /\.css$/ }),
optimization: {
splitChunks: false
},
plugins: [new SSRServerPlugin()],
}
}
// router.js
import Vue from 'vue'
import Router from 'vue-router'
import About from './views/About.vue'
Vue.use(Router)
export function createRouter() {
return new Router({
mode: 'history',
routes: [
{
path: '/',
name: 'home',
component: () => import('./views/Home.vue')
},
{
path: '/about',
name: 'about',
component: About
}
]
})
}
// main.js
import Vue from 'vue'
import App from './App.vue'
import { createRouter } from './router'
Vue.config.productionTip = false
export function createApp() {
const router = createRouter()
const app = new Vue({
router,
render: h => h(App)
})
return { app, router }
}
// entry-server.js
import { createApp } from './main'
export default (context) => new Promise((resolve, reject) => {
const { app, router } = createApp()
router.push(context.url)
router.onReady(() => {
const matchedComponents = router.getMatchedComponents()
if (!matchedComponents.length) {
return reject({ code: 404 })
}
resolve(app)
}, reject)
})
// server.js
const express = require('express')
const { createBundleRenderer } = require('vue-server-renderer')
const app = express();
const bundle = require('./dist/vue-ssr-server-bundle.json')
const renderer = createBundleRenderer(bundle, {
runInNewContext: false,
})
app.get('*', (req, res) => {
const context = { url: req.url }
renderer.renderToString(context, (err, html) => {
if (err) console.error(err)
res.end(html)
})
})
app.listen(3000)
What is expected?
Render path /, /about successfully.
What is actually happening?
- Get
ReferenceError: document is not definedwhen path/is rendered. - Render path
/aboutsuccessfully. - After deleting configureWebpack.optimization.splitChunks in vue.config.js, it shows an
Error: Server-side bundle should have one single entry file. Avoid using CommonsChunkPlugin in the server configduring building.
system: OS X 10.11.6 node: 8.11.3 vue-server-renderer: 2.5.16 webpack: 4.15.1
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 20 (3 by maintainers)
参考我的vue.config.js 配置文件
SSR真是坑,足足被坑了几个小时才解决 一直在报
Error: Server-side bundle should have one single entry file. Avoid using CommonsChunkPlugin in the server config.解决方法: 注意我的第46行配置为:
splitChunks: TARGET_NODE ? false : undefinednode环境下必须将splitChunks才不报错完整vue.config.js文件
chainWebpack: config => { if (process.env.NODE_ENV === 'production') { config.optimization.delete('splitChunks') } }This is a simple option
I switched to https://github.com/Akryum/vue-cli-plugin-ssr code splitting is working well now for me
@yekver or @vincent-yao27 did you ever overcome this issue? We’re on vue cli 3.0.3 and haven’t seen how to solve this.
I have the same error while using https://github.com/Akryum/vue-cli-plugin-ssr. Also I’ve realized that the problem is that the content of
mini-extract-css-pluginis insidevue-ssr-server-bundle.json@posva @zickat please try webpack 4 and mini-extract-css-plugin
when you write some css in each of .vue file (index or login but not App.vue)
you will find this problem “document is not defined” and see the content of vue-ssr-server-bundle.json has some document