Skip to content

Commit 8dea518

Browse files
committed
refactor: use prefetch at app root
1 parent b8e892e commit 8dea518

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

src/client/app/components/Content.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
import { h } from 'vue'
22
import { useRoute } from '../router'
3-
import { usePrefetch } from '../composables/preFetch'
43

54
export const Content = {
5+
name: 'VitePressContent',
66
setup() {
77
const route = useRoute()
8-
9-
if (import.meta.env.PROD) {
10-
// in prod mode, enable intersectionObserver based pre-fetch
11-
usePrefetch()
12-
}
13-
148
return () => (route.component ? h(route.component) : null)
159
}
1610
}

src/client/app/composables/preFetch.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Customized pre-fetch for page chunks based on
22
// https://github.com/GoogleChromeLabs/quicklink
33

4-
import { onMounted, onUnmounted, onUpdated } from 'vue'
4+
import { useRoute } from '../router'
5+
import { onMounted, onUnmounted, watch } from 'vue'
56
import { inBrowser, pathToFile } from '../utils'
67

78
const hasFetched = new Set<string>()
@@ -97,7 +98,9 @@ export function usePrefetch() {
9798
}
9899

99100
onMounted(observeLinks)
100-
onUpdated(observeLinks)
101+
102+
const route = useRoute()
103+
watch(() => route.path, observeLinks)
101104

102105
onUnmounted(() => {
103106
observer && observer.disconnect()

src/client/app/index.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { App, createApp as createClientApp, createSSRApp } from 'vue'
1+
import { App, createApp as createClientApp, createSSRApp, h } from 'vue'
22
import { inBrowser, pathToFile } from './utils'
33
import { Router, RouterSymbol, createRouter } from './router'
44
import { mixinGlobalComputed, mixinGlobalComponents } from './mixin'
@@ -7,9 +7,21 @@ import { useSiteDataByRoute } from './composables/siteDataByRoute'
77
import { usePageData } from './composables/pageData'
88
import { useUpdateHead } from './composables/head'
99
import Theme from '/@theme/index'
10+
import { usePrefetch } from './composables/preFetch'
1011

1112
const NotFound = Theme.NotFound || (() => '404 Not Found')
1213

14+
const VitePressApp = {
15+
name: 'VitePressApp',
16+
setup() {
17+
if (import.meta.env.PROD) {
18+
// in prod mode, enable intersectionObserver based pre-fetch
19+
usePrefetch()
20+
}
21+
return () => h(Theme.Layout)
22+
}
23+
}
24+
1325
export function createApp() {
1426
const router = newRouter()
1527

@@ -43,8 +55,8 @@ export function createApp() {
4355

4456
function newApp(): App {
4557
return process.env.NODE_ENV === 'production'
46-
? createSSRApp(Theme.Layout)
47-
: createClientApp(Theme.Layout)
58+
? createSSRApp(VitePressApp)
59+
: createClientApp(VitePressApp)
4860
}
4961

5062
function newRouter(): Router {

0 commit comments

Comments
 (0)