Skip to content

Commit

Permalink
fix(client): avoid mismatching between route path and page data (close
Browse files Browse the repository at this point in the history
…#1249) (#1361)

Co-authored-by: meteorlxy <meteor.lxy@foxmail.com>
  • Loading branch information
Mister-Hope and meteorlxy committed Jul 11, 2023
1 parent bd2657e commit 9b0ad9e
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions packages/client/src/router.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { pagesComponents } from '@internal/pagesComponents'
import type { PageData } from '@vuepress/shared'
import { removeEndingSlash } from '@vuepress/shared'
import {
createMemoryHistory,
Expand Down Expand Up @@ -32,13 +33,23 @@ export const createVueRouter = (): Router => {
},
})

// ensure page data and page component have been loaded in beforeResolve hook,
// but do not assign page data immediately to avoid mismatching between page data and route path.
let pendingPageData: PageData
let pendingToPath: string
router.beforeResolve(async (to, from) => {
if (to.path !== from.path || from === START_LOCATION) {
// ensure page data and page component have been loaded
;[pageData.value] = await Promise.all([
;[pendingPageData] = await Promise.all([
resolvers.resolvePageData(to.name as string),
pagesComponents[to.name as string]?.__asyncLoader(),
])
pendingToPath = to.path
}
})
// instead, assign page data in afterEach hook
router.afterEach((to, from) => {
if (to.path !== from.path && to.path === pendingToPath) {
pageData.value = pendingPageData
}
})

Expand Down

0 comments on commit 9b0ad9e

Please sign in to comment.