Skip to content

Commit 9b0ad9e

Browse files
fix(client): avoid mismatching between route path and page data (close #1249) (#1361)
Co-authored-by: meteorlxy <[email protected]>
1 parent bd2657e commit 9b0ad9e

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

packages/client/src/router.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { pagesComponents } from '@internal/pagesComponents'
2+
import type { PageData } from '@vuepress/shared'
23
import { removeEndingSlash } from '@vuepress/shared'
34
import {
45
createMemoryHistory,
@@ -32,13 +33,23 @@ export const createVueRouter = (): Router => {
3233
},
3334
})
3435

36+
// ensure page data and page component have been loaded in beforeResolve hook,
37+
// but do not assign page data immediately to avoid mismatching between page data and route path.
38+
let pendingPageData: PageData
39+
let pendingToPath: string
3540
router.beforeResolve(async (to, from) => {
3641
if (to.path !== from.path || from === START_LOCATION) {
37-
// ensure page data and page component have been loaded
38-
;[pageData.value] = await Promise.all([
42+
;[pendingPageData] = await Promise.all([
3943
resolvers.resolvePageData(to.name as string),
4044
pagesComponents[to.name as string]?.__asyncLoader(),
4145
])
46+
pendingToPath = to.path
47+
}
48+
})
49+
// instead, assign page data in afterEach hook
50+
router.afterEach((to, from) => {
51+
if (to.path !== from.path && to.path === pendingToPath) {
52+
pageData.value = pendingPageData
4253
}
4354
})
4455

0 commit comments

Comments
 (0)