SSR fails when split code
See original GitHub issueVersion
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 defined
when path/
is rendered. - Render path
/about
successfully. - 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 config
during building.
system: OS X 10.11.6 node: 8.11.3 vue-server-renderer: 2.5.16 webpack: 4.15.1
Issue Analytics
- State:
- Created 5 years ago
- Comments:20 (3 by maintainers)
Top Results From Across the Web
SSR fails after splitting code - Get Help - Vue Forum
I just split the code of a popular ssr example by changing 2 lines of codes, then npm run build and npm start...
Read more >SSRS Split expression #Error - Stack Overflow
The expression is made on a textbox and I need to have this function cause our costumer requires it. I want this input...
Read more >Why code splitting is hard in react server side rendering?
Recap to our initial problem statement, we would like to do code splitting during Server side rendering or commonly termed as SSR.
Read more >What's the point of SSR when code splitting/lazy loading exists ...
To me, SSR and client side rendering (again especially with code splitting and lazy loading) falls under same category, unless SEO is necessary, ......
Read more >Inside the Code Split - DEV Community
The problem with SSR is a hydrate function - you have to load "everything you need", before rendering on the Client "the same...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
参考我的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 : undefined
node环境下必须将splitChunks才不报错完整vue.config.js文件
chainWebpack: config => { if (process.env.NODE_ENV === 'production') { config.optimization.delete('splitChunks') } }
This is a simple option