Skip to content

Commit

Permalink
fix(client): avoid updating routeLocale on route hash change (#1253)
Browse files Browse the repository at this point in the history
Co-authored-by: Xinyu Liu <meteor.lxy@foxmail.com>
  • Loading branch information
Mister-Hope and meteorlxy committed Feb 16, 2023
1 parent 5019fc0 commit 5eb9489
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
14 changes: 9 additions & 5 deletions packages/client/src/setupGlobalComputed.ts
@@ -1,4 +1,4 @@
import { type App, computed } from 'vue'
import { type App, computed, ref, watch } from 'vue'
import type { Router } from 'vue-router'
import {
type LayoutsRef,
Expand Down Expand Up @@ -58,13 +58,17 @@ export const setupGlobalComputed = (
router: Router,
clientConfigs: ClientConfig[]
): GlobalComputed => {
// create a manual computed route path, so that route hash changes won't trigger all downstream computed
const routePath = ref(router.currentRoute.value.path)
watch(
() => router.currentRoute.value.path,
(value) => (routePath.value = value)
)

// create global computed
const layouts = computed(() => resolvers.resolveLayouts(clientConfigs))
const routeLocale = computed(() =>
resolvers.resolveRouteLocale(
siteData.value.locales,
router.currentRoute.value.path
)
resolvers.resolveRouteLocale(siteData.value.locales, routePath.value)
)
const siteLocaleData = computed(() =>
resolvers.resolveSiteLocaleData(siteData.value, routeLocale.value)
Expand Down
8 changes: 1 addition & 7 deletions packages/client/src/setupUpdateHead.ts
@@ -1,7 +1,6 @@
import { isPlainObject, isString } from '@vuepress/shared'
import type { HeadConfig, VuepressSSRContext } from '@vuepress/shared'
import { onMounted, provide, ref, useSSRContext, watch } from 'vue'
import { useRoute } from 'vue-router'
import {
updateHeadSymbol,
usePageHead,
Expand All @@ -13,7 +12,6 @@ import type { UpdateHead } from './composables/index.js'
* Auto update head and provide as global util
*/
export const setupUpdateHead = (): void => {
const route = useRoute()
const head = usePageHead()
const lang = usePageLang()

Expand Down Expand Up @@ -64,11 +62,7 @@ export const setupUpdateHead = (): void => {
loadHead()
updateHead()
watch(
// when watching `head`, route hash changes will also trigger the watcher,
// causing unnecessary head updates
// so we watch `head` in dev mode to support hot-reload of `frontmatter.head`,
// and watch `route.path` in production mode to avoid extra updates
() => (__VUEPRESS_DEV__ ? head.value : route.path),
() => head.value,
() => updateHead()
)
})
Expand Down

0 comments on commit 5eb9489

Please sign in to comment.