Skip to content

Commit 484b064

Browse files
committed
refactor(client): use customRef for pageChunk
1 parent b352c09 commit 484b064

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

packages/client/src/setupGlobalComputed.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { App } from 'vue'
2-
import { computed } from 'vue'
2+
import { computed, customRef } from 'vue'
33
import type { Router } from 'vue-router'
44
import { clientDataSymbol } from './composables/index.js'
55
import { redirects, routes } from './internal/routes.js'
@@ -8,6 +8,7 @@ import { resolvers } from './resolvers.js'
88
import type {
99
ClientConfig,
1010
ClientData,
11+
PageChunk,
1112
PageData,
1213
PageFrontmatter,
1314
PageHead,
@@ -31,19 +32,29 @@ export const setupGlobalComputed = (
3132
const routePath = computed(() => router.currentRoute.value.path)
3233

3334
// load page chunk from route meta
34-
const pageChunk = computed(() => router.currentRoute.value.meta._pageChunk!)
35+
const pageChunk = customRef<PageChunk>((track, trigger) => ({
36+
get() {
37+
track()
38+
return router.currentRoute.value.meta._pageChunk!
39+
},
40+
set(value) {
41+
router.currentRoute.value.meta._pageChunk = value
42+
trigger()
43+
},
44+
}))
3545

3646
// handle page data HMR
3747
if (__VUEPRESS_DEV__ && (import.meta.webpackHot || import.meta.hot)) {
3848
__VUE_HMR_RUNTIME__.updatePageData = async (newPageData: PageData) => {
3949
const oldPageChunk = await routes.value[newPageData.path].loader()
50+
const newPageChunk = { comp: oldPageChunk.comp, data: newPageData }
4051
routes.value[newPageData.path].loader = () =>
41-
Promise.resolve({ comp: oldPageChunk.comp, data: newPageData })
52+
Promise.resolve(newPageChunk)
4253
if (
4354
newPageData.path ===
4455
router.currentRoute.value.meta._pageChunk?.data.path
4556
) {
46-
router.currentRoute.value.meta._pageChunk.data = newPageData
57+
pageChunk.value = newPageChunk
4758
}
4859
}
4960
}

0 commit comments

Comments
 (0)