Skip to content

Commit

Permalink
fix(vue-app): use app.context.route for resolving components (#9050)
Browse files Browse the repository at this point in the history
  • Loading branch information
enwin authored Apr 1, 2021
1 parent c04a793 commit 04d3382
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
10 changes: 4 additions & 6 deletions packages/vue-app/template/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
32 changes: 16 additions & 16 deletions packages/vue-app/template/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,26 +267,26 @@ async function createApp(ssrContext, config = {}) {
}
}

// If server-side, wait for async component to be resolved first
if (process.server && ssrContext && ssrContext.url) {
await new Promise((resolve, reject) => {
router.push(ssrContext.url, 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()
// Wait for async component to be resolved first
await new Promise((resolve, reject) => {
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) => {
// 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 || {}
unregister()
resolve()
})
}
app.context.route = await getRouteData(to)
app.context.params = to.params || {}
app.context.query = to.query || {}
unregister()
resolve()
})
})
}
})

return {
<% if(store) { %>store,<% } %>
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/spa-base.browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down

0 comments on commit 04d3382

Please sign in to comment.