Skip to content

Commit 1aeaa14

Browse files
authored
Merge pull request #2154 from didi/fix-ssr-import-components-error
feat: 新增useSSR配置,规避ssr模式下异步分包hydrate报错问题
2 parents 1731e80 + 66cee48 commit 1aeaa14

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

docs-vitepress/api/compile.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -824,8 +824,13 @@ module.exports = defineConfig({
824824

825825
transRpxFn 配置用于自定义输出 web 时对于 rpx 样式单位的转换逻辑,常见的方式有转换为 vw 或转换为 rem
826826

827+
`{useSSR: boolean}`
828+
829+
useSSR 默认值为 `false`,当 SSR 模式下使用异步分包时,需要将 useSSR 设置为 `true`, 其他场景不需要。
830+
831+
827832
```js
828-
// vue.config.js
833+
// mpx.config.js
829834
module.exports = defineConfig({
830835
pluginOptions: {
831836
mpx: {
@@ -834,7 +839,9 @@ module.exports = defineConfig({
834839
transRpxFn: function (match, $1) {
835840
if ($1 === '0') return $1
836841
return `${$1 * +(100 / 750).toFixed(8)}vw`
837-
}
842+
},
843+
// 当 SSR 模式下使用异步分包时
844+
useSSR: true
838845
}
839846
}
840847
}

packages/webpack-plugin/lib/runtime/optionProcessor.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ function createApp ({ componentsMap, Vue, pagesMap, firstPage, VueRouter, App, t
352352
return extend({ app }, option)
353353
}
354354

355-
export function processAppOption ({ firstPage, pagesMap, componentsMap, App, Vue, VueRouter, tabBarMap, el }) {
355+
export function processAppOption ({ firstPage, pagesMap, componentsMap, App, Vue, VueRouter, tabBarMap, el, useSSR }) {
356356
if (!isBrowser) {
357357
return context => {
358358
const { app, router, pinia = {} } = createApp({
@@ -379,7 +379,7 @@ export function processAppOption ({ firstPage, pagesMap, componentsMap, App, Vue
379379
}
380380
}
381381
} else {
382-
const { app, pinia } = createApp({
382+
const { app, pinia, router } = createApp({
383383
App,
384384
componentsMap,
385385
Vue,
@@ -391,6 +391,14 @@ export function processAppOption ({ firstPage, pagesMap, componentsMap, App, Vue
391391
if (window.__INITIAL_STATE__ && pinia) {
392392
pinia.state.value = window.__INITIAL_STATE__
393393
}
394-
app.$mount(el)
394+
if (useSSR) {
395+
// https://v3.router.vuejs.org/api/#router-onready
396+
// ssr 场景如果使用了异步组件,需要在 onReady 回调中挂载,否则 hydrate 可能会报错
397+
router.onReady(() => {
398+
app.$mount(el)
399+
})
400+
} else {
401+
app.$mount(el)
402+
}
395403
}
396404
}

packages/webpack-plugin/lib/web/processMainScript.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ export default processAppOption({
7171
componentsMap: ${shallowStringify(componentsMap)},
7272
Vue: Vue,
7373
VueRouter: VueRouter,
74-
el: ${JSON.stringify(webConfig.el || '#app')}
74+
el: ${JSON.stringify(webConfig.el || '#app')},
75+
useSSR: ${JSON.stringify(!!webConfig.useSSR)}
7576
})\n`
7677

7778
callback(null, {

0 commit comments

Comments
 (0)