提交 feb74ebe 编写于 作者: U ULIVZ

fix($core): SSR error when async components were registered in runtime.

上级 6550f868
......@@ -15,8 +15,14 @@ export default {
render (h, { parent, props, data }) {
const pageKey = props.pageKey || parent.$page.key
if (components[pageKey]) {
Vue.component(pageKey, components[pageKey])
// In SSR, if a component is not registered with the component option
// vue-server-renderer will not be able to resovle it.
if (!parent.$ssrContext) {
Vue.component(pageKey, components[pageKey])
}
return h(pageKey, {
class: [props.custom ? 'custom' : '', data.class, data.staticClass],
style: data.style,
......
import Vue from 'vue'
import { createApp } from './app'
import pageComponents from '@internal/page-components'
import layoutComponents from '@internal/layout-components'
export default context => new Promise((resolve, reject) => {
const { app, router } = createApp(true /* isServer */)
......@@ -10,5 +13,26 @@ export default context => new Promise((resolve, reject) => {
}
router.push(url)
router.onReady(() => resolve(app))
// In SSR, if a component is not registered with the component option,
// vue-server-renderer will not able to resolve it.
//
// Build also works after deleting this, but the content of all pages
// will not appear to the output html, which is not conducive to SEO.
const asyncComponentLoadingPromises = [
...getComponentArr(pageComponents),
...getComponentArr(layoutComponents)
].map(({ name, loadFn }) => {
return loadFn.then(comp => {
Vue.component(name, comp.default)
})
})
router.onReady(() => {
Promise.all(asyncComponentLoadingPromises).then(() => resolve(app))
})
})
function getComponentArr (components) {
return Object.keys(components).map(name => ({ name, loadFn: components[name] }))
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册