fix(vue-app): use app.context.route for resolving components (#9050)

This commit is contained in:
Thomas Beduneau 2021-04-01 12:00:51 +02:00 committed by GitHub
parent c04a793b4a
commit 04d33829f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 23 deletions

View File

@ -229,10 +229,8 @@ function applySSRData (Component, ssrData) {
}
// Get matched components
function resolveComponents (router) {
const path = getLocation(router.options.base, router.options.mode)
return flatMapComponents(router.match(path), async (Component, _, match, key, index) => {
function resolveComponents (route) {
return flatMapComponents(route, async (Component, _, match, key, index) => {
// If component is not resolved yet, resolve it
if (typeof Component === 'function' && !Component.options) {
Component = await Component()
@ -884,7 +882,7 @@ async function mountApp (__app) {
}
<% if (features.transitions) { %>
// Resolve route components
const Components = await Promise.all(resolveComponents(router))
const Components = await Promise.all(resolveComponents(app.context.route))
// Enable transitions
_app.setTransitions = _app.$options.nuxt.setTransitions.bind(_app)
@ -893,7 +891,7 @@ async function mountApp (__app) {
_lastPaths = router.currentRoute.matched.map(route => compile(route.path)(router.currentRoute.params))
}
<% } else if (features.asyncData || features.fetch) { %>
await Promise.all(resolveComponents(router))
await Promise.all(resolveComponents(app.context.route))
<% } %>
// Initialize error handler
_app.$loading = {} // To avoid error while _app.$nuxt does not exist

View File

@ -267,17 +267,18 @@ async function createApp(ssrContext, config = {}) {
}
}
// If server-side, wait for async component to be resolved first
if (process.server && ssrContext && ssrContext.url) {
// Wait for async component to be resolved first
await new Promise((resolve, reject) => {
router.push(ssrContext.url, resolve, (err) => {
router.push(app.context.route.fullPath, resolve, (err) => {
// https://github.com/vuejs/vue-router/blob/v3.4.3/src/util/errors.js
if (!err._isRouter) return reject(err)
if (err.type !== 2 /* NavigationFailureType.redirected */) return resolve()
// navigated to a different route in router guard
const unregister = router.afterEach(async (to, from) => {
if (process.server && ssrContext && ssrContext.url) {
ssrContext.url = to.fullPath
}
app.context.route = await getRouteData(to)
app.context.params = to.params || {}
app.context.query = to.query || {}
@ -286,7 +287,6 @@ async function createApp(ssrContext, config = {}) {
})
})
})
}
return {
<% if(store) { %>store,<% } %>

View File

@ -26,7 +26,7 @@ describe('spa router base browser', () => {
test('Open /app (router base)', async () => {
page = await browser.page(url('/app'))
expect(await page.evaluate(() => location.href)).toBe(url('/app'))
expect(await page.evaluate(() => location.href)).toBe(url('/app/'))
expect(await page.html()).not.toContain('This page could not be found')