Skip to content

Commit f8e42bc

Browse files
committed
feat($core): 'contentLoading' site config option
1 parent feccffb commit f8e42bc

File tree

7 files changed

+65
-9
lines changed

7 files changed

+65
-9
lines changed

packages/@vuepress/core/lib/app/app.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global VUEPRESS_TEMP_PATH */
1+
/* global VUEPRESS_TEMP_PATH, CONTENT_LOADING */
22
import Vue from 'vue'
33
import Router from 'vue-router'
44
import dataMixin from './dataMixin'
@@ -10,7 +10,8 @@ import ClientComputedMixin from '@transform/ClientComputedMixin'
1010
import Store from './plugins/Store'
1111

1212
// built-in components
13-
import Content from './components/Content'
13+
import LoadableContent from './components/Content.vue'
14+
import Content from './components/Content.js'
1415
import ContentSlotsDistributor from './components/ContentSlotsDistributor'
1516
import OutboundLink from './components/OutboundLink.vue'
1617
import ClientOnly from './components/ClientOnly'
@@ -35,7 +36,12 @@ Vue.use(Store, '$vuepress')
3536
// mixin for exposing $site and $page
3637
Vue.mixin(dataMixin(ClientComputedMixin, siteData))
3738
// component for rendering markdown content and setting title etc.
38-
Vue.component('Content', Content)
39+
if (CONTENT_LOADING) {
40+
Vue.component('Content', LoadableContent)
41+
} else {
42+
Vue.component('Content', Content)
43+
}
44+
3945
Vue.component('ContentSlotsDistributor', ContentSlotsDistributor)
4046
Vue.component('OutboundLink', OutboundLink)
4147
// component for client-only content
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import Vue from 'vue'
2+
import components from '@internal/page-components'
3+
4+
export default {
5+
functional: true,
6+
props: {
7+
pageKey: String,
8+
slotKey: String
9+
},
10+
render (h, { parent, props, data }) {
11+
const pageKey = props.pageKey || parent.$page.key
12+
if (components[pageKey]) {
13+
// In SSR, if a component is not registered with the component option
14+
// vue-server-renderer will not be able to resovle it.
15+
if (!parent.$ssrContext) {
16+
Vue.component(pageKey, components[pageKey])
17+
}
18+
return h(pageKey, {
19+
class: [data.class, data.staticClass],
20+
style: data.style,
21+
props: {
22+
slotKey: props.slot || 'default'
23+
}
24+
})
25+
}
26+
return h('')
27+
}
28+
}

packages/@vuepress/core/lib/app/components/Content.vue

+9-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@
1010
</template>
1111

1212
<script>
13+
/* global CONTENT_LOADING */
14+
1315
import Vue from 'vue'
1416
import components from '@internal/page-components'
1517
import ContentLoading from './ContentLoading'
1618
19+
const CONTENT_LOADING_COMPONENT = typeof CONTENT_LOADING === 'string'
20+
? CONTENT_LOADING
21+
: 'ContentLoading'
22+
1723
export default {
1824
components: { ContentLoading },
1925
@@ -24,7 +30,7 @@ export default {
2430
2531
data () {
2632
return {
27-
layout: 'ContentLoading',
33+
layout: CONTENT_LOADING_COMPONENT,
2834
noTransition: true
2935
}
3036
},
@@ -34,7 +40,7 @@ export default {
3440
return this.pageKey || this.$page.key
3541
},
3642
disableTransition () {
37-
return !this.layout || this.layout === 'ContentLoading' || this.noTransition
43+
return !this.layout || this.layout === CONTENT_LOADING_COMPONENT || this.noTransition
3844
}
3945
},
4046
@@ -69,7 +75,7 @@ export default {
6975
this.noTransition = true
7076
return
7177
}
72-
this.layout = 'ContentLoading'
78+
this.layout = CONTENT_LOADING_COMPONENT
7379
if (components[pageKey]) {
7480
this.noTransition = false
7581
if (!this.$ssrContext) {

packages/@vuepress/core/lib/webpack/createBaseConfig.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ module.exports = function createBaseConfig ({
300300
.use(require('webpack/lib/DefinePlugin'), [{
301301
VUEPRESS_VERSION: JSON.stringify(require('../../package.json').version),
302302
VUEPRESS_TEMP_PATH: JSON.stringify(tempPath),
303-
LAST_COMMIT_HASH: JSON.stringify(getLastCommitHash())
303+
LAST_COMMIT_HASH: JSON.stringify(getLastCommitHash()),
304+
CONTENT_LOADING: JSON.stringify(siteConfig.contentLoading || false)
304305
}])
305306

306307
pluginAPI.options.define.apply(config)

packages/docs/docs/.vuepress/config.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const container = require('markdown-it-container')
33

44
module.exports = ctx => ({
55
dest: '../../vuepress',
6+
contentLoading: true,
67
locales: {
78
'/': {
89
lang: 'en-US',

packages/docs/docs/config/README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ Specify locales for i18n support. For more details, see the guide on [Internatio
118118

119119
A function to control what files should have `<link rel="preload">` resource hints generated. See [shouldPrefetch](https://ssr.vuejs.org/api/#shouldprefetch).
120120

121+
### contentLoading
122+
123+
- Type: `boolean|string`
124+
- Default: `false`
125+
126+
Whether to enable placeholder loading for asynchronous loading content. if it's a stirng, it should be the name of your custom loading component.
127+
121128
## Styling
122129

123130
### palette.styl
@@ -135,7 +142,7 @@ $codeBgColor = #282c34
135142
```
136143

137144
::: danger Note
138-
You should ONLY write color variables in this file. since `palette.styl` will be imported at the end of the root stylus config file, as a config, it will be used by multiple files, so once you wrote styles here, your style would be duplicated by multiple times.
145+
You should ONLY write color variables in this file. since `palette.styl` will be imported at the end of the root stylus config file, as a config, it will be used by multiple files, so once you wrote styles here, your style would be duplicated by multiple times.
139146
:::
140147

141148
### index.styl

packages/docs/docs/zh/config/README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ module.exports = {
115115

116116
一个函数,用来控制对于哪些文件,是需要生成 `<link rel="prefetch">` 资源提示的。请参考 [shouldPrefetch](https://ssr.vuejs.org/zh/api/#shouldpreload)
117117

118+
### contentLoading
119+
120+
- 类型: `boolean|string`
121+
- 默认值: `false`
122+
123+
是否对异步加载页面的内容开启占位符加载。如果它是一个字符串,那么它应该是自定义加载组件的名称。
124+
118125
## Styling
119126

120127
> To be translated.
@@ -134,7 +141,7 @@ $codeBgColor = #282c34
134141
```
135142

136143
::: danger Note
137-
You should ONLY write color variables in this file. since `palette.styl` will be imported at the end of the root stylus config file, as a config, it will be used by multiple files, so once you wrote styles here, your style would be duplicated by multiple times.
144+
You should ONLY write color variables in this file. since `palette.styl` will be imported at the end of the root stylus config file, as a config, it will be used by multiple files, so once you wrote styles here, your style would be duplicated by multiple times.
138145
:::
139146

140147
### index.styl

0 commit comments

Comments
 (0)