Skip to content

Commit 9d85d4f

Browse files
committed
perf(client): use eager computed for routeLocale
1 parent 08cf419 commit 9d85d4f

File tree

5 files changed

+14
-16
lines changed

5 files changed

+14
-16
lines changed

packages/client/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"dependencies": {
4040
"@vue/devtools-api": "^6.5.0",
4141
"@vuepress/shared": "workspace:*",
42+
"@vueuse/core": "^10.2.1",
4243
"vue": "^3.3.4",
4344
"vue-router": "^4.2.2"
4445
},

packages/client/src/composables/routeLocale.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { inject } from 'vue'
2-
import type { ComputedRef, InjectionKey } from 'vue'
2+
import type { InjectionKey, Ref } from 'vue'
33

44
/**
55
* Route locale path
@@ -9,7 +9,7 @@ export type RouteLocale = string
99
/**
1010
* Ref wrapper of `RouteLocale`
1111
*/
12-
export type RouteLocaleRef = ComputedRef<RouteLocale>
12+
export type RouteLocaleRef = Readonly<Ref<RouteLocale>>
1313

1414
/**
1515
* Injection key for page route locale path

packages/client/src/setupGlobalComputed.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { type App, computed, ref, watch } from 'vue'
1+
import { computedEager } from '@vueuse/core'
2+
import { type App, computed } from 'vue'
23
import type { Router } from 'vue-router'
34
import {
45
type LayoutsRef,
@@ -58,17 +59,13 @@ export const setupGlobalComputed = (
5859
router: Router,
5960
clientConfigs: ClientConfig[]
6061
): 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-
6862
// create global computed
6963
const layouts = computed(() => resolvers.resolveLayouts(clientConfigs))
70-
const routeLocale = computed(() =>
71-
resolvers.resolveRouteLocale(siteData.value.locales, routePath.value)
64+
const routeLocale = computedEager(() =>
65+
resolvers.resolveRouteLocale(
66+
siteData.value.locales,
67+
router.currentRoute.value.path
68+
)
7269
)
7370
const siteLocaleData = computed(() =>
7471
resolvers.resolveSiteLocaleData(siteData.value, routeLocale.value)

packages/client/src/setupUpdateHead.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,7 @@ export const setupUpdateHead = (): void => {
6161
onMounted(() => {
6262
loadHead()
6363
updateHead()
64-
watch(
65-
() => head.value,
66-
() => updateHead()
67-
)
64+
watch(() => head.value, updateHead)
6865
})
6966
}
7067

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)