Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(client): avoid mismatching between route path and page data (close #1249) #1361

Merged
merged 3 commits into from
Jul 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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