Skip to content

Commit d08c777

Browse files
committed
refactor: refine $vuepress client plugin.
1 parent b6e775c commit d08c777

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

Diff for: packages/@vuepress/core/lib/app/components/Content.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import Vue from 'vue'
2-
import components from '@internal/page-components'
32

43
export default {
54
functional: true,
@@ -9,12 +8,13 @@ export default {
98
},
109
render (h, { parent, props, data }) {
1110
const pageKey = props.pageKey || parent.$page.key
12-
if (components[pageKey]) {
11+
if (Vue.$vuepress.isPageExists(pageKey)) {
1312
// In SSR, if a component is not registered with the component option
1413
// vue-server-renderer will not be able to resovle it.
1514
if (!parent.$ssrContext) {
16-
Vue.component(pageKey, components[pageKey])
15+
Vue.$vuepress.registerPageAsyncComponent(pageKey)
1716
}
17+
1818
return h(pageKey, {
1919
class: [data.class, data.staticClass],
2020
style: data.style,

Diff for: packages/@vuepress/core/lib/app/components/Content.vue

+7-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
/* global CONTENT_LOADING */
1414
1515
import Vue from 'vue'
16-
import components from '@internal/page-components'
1716
import ContentLoading from './ContentLoading'
1817
1918
const CONTENT_LOADING_COMPONENT = typeof CONTENT_LOADING === 'string'
@@ -61,26 +60,28 @@ export default {
6160
methods: {
6261
loadContent (pageKey) {
6362
this.layout = null
64-
if (components[pageKey]) {
63+
if (this.$vuepress.isPageExists(pageKey)) {
6564
if (!this.$ssrContext) {
66-
Vue.component(pageKey, components[pageKey])
65+
this.$vuepress.registerPageAsyncComponent(pageKey)
6766
this.layout = pageKey
6867
}
6968
}
7069
},
7170
7271
reloadContent (pageKey) {
73-
if (Vue.component(pageKey)) {
72+
// When page has been loaded, disable transition.
73+
if (this.$vuepress.isPageLoaded(pageKey)) {
7474
this.layout = pageKey
7575
this.noTransition = true
7676
return
7777
}
78+
// Start to load unfetched page component.
7879
this.layout = CONTENT_LOADING_COMPONENT
79-
if (components[pageKey]) {
80+
if (this.$vuepress.isPageExists(pageKey)) {
8081
this.noTransition = false
8182
if (!this.$ssrContext) {
8283
Promise.all([
83-
components[pageKey](),
84+
this.$vuepress.loadPageAsyncComponent(pageKey),
8485
new Promise(resolve => setTimeout(resolve, 300))
8586
]).then(([comp]) => {
8687
this.$vuepress.$emit('AsyncMarkdownAssetLoaded', this.pageKey)
+10-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import { Store } from './Store';
2+
import { AsyncComponent } from 'vue'
23

34
declare class VuePress extends Store {
5+
isPageExists (pageKey: string): boolean;
46

7+
isPageLoaded (pageKey: string): boolean;
8+
9+
getPageAsyncComponent (pageKey: string): () => Promise<AsyncComponent>;
10+
11+
loadPageAsyncComponent (pageKey: string): Promise<AsyncComponent>;
12+
13+
registerPageAsyncComponent (pageKey: string): void;
514
}
615

716
declare module "vue/types/vue" {
817
interface Vue {
9-
$vuepress: Store;
18+
$vuepress: VuePress;
1019
}
1120
}

Diff for: packages/@vuepress/core/lib/app/plugins/VuePress.js

+23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
1+
import Vue from 'vue'
12
import Store from './Store'
3+
import pageComponents from '@internal/page-components'
24

35
class VuePress extends Store {
6+
isPageExists (pageKey) {
7+
return Boolean(pageComponents[pageKey])
8+
}
9+
10+
isPageLoaded (pageKey) {
11+
return Boolean(Vue.component(pageKey))
12+
}
13+
14+
getPageAsyncComponent (pageKey) {
15+
if (!this.isPageExists(pageKey)) {
16+
throw new Error(`Cannot found ${pageKey}`)
17+
}
18+
return pageComponents[pageKey]
19+
}
20+
21+
loadPageAsyncComponent (pageKey) {
22+
return this.getPageAsyncComponent(pageKey)()
23+
}
424

25+
registerPageAsyncComponent (pageKey) {
26+
Vue.component(pageKey, this.getPageAsyncComponent(pageKey))
27+
}
528
}
629

730
export default {

0 commit comments

Comments
 (0)