Skip to content

Commit 5eb9489

Browse files
fix(client): avoid updating routeLocale on route hash change (#1253)
Co-authored-by: Xinyu Liu <meteor.lxy@foxmail.com>
1 parent 5019fc0 commit 5eb9489

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

packages/client/src/setupGlobalComputed.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type App, computed } from 'vue'
1+
import { type App, computed, ref, watch } from 'vue'
22
import type { Router } from 'vue-router'
33
import {
44
type LayoutsRef,
@@ -58,13 +58,17 @@ export const setupGlobalComputed = (
5858
router: Router,
5959
clientConfigs: ClientConfig[]
6060
): GlobalComputed => {
61+
// create a manual computed route path, so that route hash changes won't trigger all downstream computed
62+
const routePath = ref(router.currentRoute.value.path)
63+
watch(
64+
() => router.currentRoute.value.path,
65+
(value) => (routePath.value = value)
66+
)
67+
6168
// create global computed
6269
const layouts = computed(() => resolvers.resolveLayouts(clientConfigs))
6370
const routeLocale = computed(() =>
64-
resolvers.resolveRouteLocale(
65-
siteData.value.locales,
66-
router.currentRoute.value.path
67-
)
71+
resolvers.resolveRouteLocale(siteData.value.locales, routePath.value)
6872
)
6973
const siteLocaleData = computed(() =>
7074
resolvers.resolveSiteLocaleData(siteData.value, routeLocale.value)

packages/client/src/setupUpdateHead.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { isPlainObject, isString } from '@vuepress/shared'
22
import type { HeadConfig, VuepressSSRContext } from '@vuepress/shared'
33
import { onMounted, provide, ref, useSSRContext, watch } from 'vue'
4-
import { useRoute } from 'vue-router'
54
import {
65
updateHeadSymbol,
76
usePageHead,
@@ -13,7 +12,6 @@ import type { UpdateHead } from './composables/index.js'
1312
* Auto update head and provide as global util
1413
*/
1514
export const setupUpdateHead = (): void => {
16-
const route = useRoute()
1715
const head = usePageHead()
1816
const lang = usePageLang()
1917

@@ -64,11 +62,7 @@ export const setupUpdateHead = (): void => {
6462
loadHead()
6563
updateHead()
6664
watch(
67-
// when watching `head`, route hash changes will also trigger the watcher,
68-
// causing unnecessary head updates
69-
// so we watch `head` in dev mode to support hot-reload of `frontmatter.head`,
70-
// and watch `route.path` in production mode to avoid extra updates
71-
() => (__VUEPRESS_DEV__ ? head.value : route.path),
65+
() => head.value,
7266
() => updateHead()
7367
)
7468
})

0 commit comments

Comments
 (0)