Skip to content

Commit 0a7d85b

Browse files
authored
feat: remove content loading and refine scroll behavior (#1117)
1 parent 1c2aa08 commit 0a7d85b

File tree

18 files changed

+95
-313
lines changed

18 files changed

+95
-313
lines changed

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

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

1212
// built-in components
13-
import LoadableContent from './components/Content.vue'
1413
import Content from './components/Content.js'
1514
import ContentSlotsDistributor from './components/ContentSlotsDistributor'
1615
import OutboundLink from './components/OutboundLink.vue'
@@ -36,12 +35,8 @@ Vue.use(VuePress)
3635
// mixin for exposing $site and $page
3736
Vue.mixin(dataMixin(ClientComputedMixin, siteData))
3837
// component for rendering markdown content and setting title etc.
39-
if (CONTENT_LOADING) {
40-
Vue.component('Content', LoadableContent)
41-
} else {
42-
Vue.component('Content', Content)
43-
}
4438

39+
Vue.component('Content', Content)
4540
Vue.component('ContentSlotsDistributor', ContentSlotsDistributor)
4641
Vue.component('OutboundLink', OutboundLink)
4742
// component for client-only content
@@ -67,6 +62,9 @@ export function createApp (isServer) {
6762
if (savedPosition) {
6863
return savedPosition
6964
} else if (to.hash) {
65+
if (Vue.$vuepress.$get('disableScrollBehavior')) {
66+
return false
67+
}
7068
return {
7169
selector: to.hash
7270
}

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

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Vue from 'vue'
1+
import { isPageExists } from '../util'
22

33
export default {
44
props: {
@@ -10,13 +10,7 @@ export default {
1010
},
1111
render (h) {
1212
const pageKey = this.pageKey || this.$parent.$page.key
13-
if (Vue.$vuepress.isPageExists(pageKey)) {
14-
// In SSR, if a component is not registered with the component option
15-
// vue-server-renderer will not be able to resovle it.
16-
if (!this.$parent.$ssrContext) {
17-
Vue.$vuepress.registerPageAsyncComponent(pageKey)
18-
}
19-
13+
if (isPageExists(pageKey)) {
2014
return h(pageKey)
2115
}
2216
return h('')

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

-114
This file was deleted.

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

-39
This file was deleted.

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

+1-26
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,6 @@
1-
import Vue from 'vue'
21
import Store from './Store'
3-
import pageComponents from '@internal/page-components'
42

5-
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-
}
24-
25-
registerPageAsyncComponent (pageKey) {
26-
Vue.component(pageKey, this.getPageAsyncComponent(pageKey))
27-
}
28-
}
3+
class VuePress extends Store {}
294

305
export default {
316
install (Vue) {
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import Vue from 'vue'
21
import { createApp } from './app'
3-
import pageComponents from '@internal/page-components'
4-
import layoutComponents from '@internal/layout-components'
52

63
export default context => new Promise((resolve, reject) => {
74
const { app, router } = createApp(true /* isServer */)
@@ -13,26 +10,5 @@ export default context => new Promise((resolve, reject) => {
1310
}
1411

1512
router.push(url)
16-
17-
// In SSR, if a component is not registered with the component option,
18-
// vue-server-renderer will not able to resolve it.
19-
//
20-
// Build also works after deleting this, but the content of all pages
21-
// will not appear to the output html, which is not conducive to SEO.
22-
const asyncComponentLoadingPromises = [
23-
...getComponentArr(pageComponents),
24-
...getComponentArr(layoutComponents)
25-
].map(({ name, loadFn }) => {
26-
return loadFn().then(comp => {
27-
Vue.component(name, comp.default)
28-
})
29-
})
30-
31-
router.onReady(() => {
32-
Promise.all(asyncComponentLoadingPromises).then(() => resolve(app))
33-
})
13+
router.onReady(() => resolve(app))
3414
})
35-
36-
function getComponentArr (components) {
37-
return Object.keys(components).map(name => ({ name, loadFn: components[name] }))
38-
}

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

+39
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,42 @@
1+
import Vue from 'vue'
2+
import layoutComponents from '@internal/layout-components'
3+
import pageComponents from '@internal/page-components'
4+
5+
const asyncComponents = Object.assign({}, layoutComponents, pageComponents)
6+
7+
export function isPageExists (pageKey) {
8+
return Boolean(pageComponents[pageKey])
9+
}
10+
11+
export function isPageLoaded (pageKey) {
12+
return Boolean(Vue.component(pageKey))
13+
}
14+
15+
export function getPageAsyncComponent (pageKey) {
16+
return pageComponents[pageKey]
17+
}
18+
19+
export function isLayoutExists (layout) {
20+
return Boolean(layoutComponents[layout])
21+
}
22+
23+
export function isLayoutLoaded (layout) {
24+
return Boolean(Vue.component(layout))
25+
}
26+
27+
export function getLayoutAsyncComponent (pageKey) {
28+
return layoutComponents[pageKey]
29+
}
30+
31+
export function ensureAsyncComponentsLoaded (...names) {
32+
return Promise.all(names.filter(v => v).map(async (name) => {
33+
if (!Vue.component(name) && asyncComponents[name]) {
34+
const comp = await asyncComponents[name]()
35+
Vue.component(name, comp.default)
36+
}
37+
}))
38+
}
39+
140
/**
241
* Inject option to Vue SFC
342
* @param {object} options

packages/@vuepress/core/lib/internal-plugins/routes.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@ module.exports = (options, ctx) => ({
1414
*/
1515
function importCode () {
1616
return `
17-
import { injectComponentOption } from '@app/util'
17+
import { injectComponentOption, ensureAsyncComponentsLoaded } from '@app/util'
1818
import rootMixins from '@internal/root-mixins'
1919
import layoutComponents from '@internal/layout-components'
2020
import pageComponents from '@internal/page-components'
2121
import LayoutDistributor from '@app/components/LayoutDistributor.vue'
2222
2323
injectComponentOption(LayoutDistributor, 'mixins', rootMixins)
24-
injectComponentOption(LayoutDistributor, 'components', Object.assign({}, layoutComponents, pageComponents))
2524
`
2625
}
2726

@@ -34,14 +33,20 @@ function routesCode (pages) {
3433
function genRoute ({
3534
path: pagePath,
3635
key: componentName,
36+
frontmatter: {
37+
layout
38+
},
3739
regularPath,
3840
_meta
3941
}) {
4042
let code = `
4143
{
4244
name: ${JSON.stringify(componentName)},
4345
path: ${JSON.stringify(pagePath)},
44-
component: LayoutDistributor,${_meta ? `\n meta: ${JSON.stringify(_meta)}` : ''}
46+
component: LayoutDistributor,
47+
beforeEnter: (to, from, next) => {
48+
ensureAsyncComponentsLoaded(${JSON.stringify(layout || 'Layout')}, ${JSON.stringify(componentName)}).then(next)
49+
},${_meta ? `\n meta: ${JSON.stringify(_meta)}` : ''}
4550
}`
4651

4752
const dncodedPath = decodeURIComponent(pagePath)

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,7 @@ module.exports = function createBaseConfig ({
285285
.use(require('webpack/lib/DefinePlugin'), [{
286286
VUEPRESS_VERSION: JSON.stringify(require('../../package.json').version),
287287
VUEPRESS_TEMP_PATH: JSON.stringify(tempPath),
288-
LAST_COMMIT_HASH: JSON.stringify(getLastCommitHash()),
289-
CONTENT_LOADING: JSON.stringify(siteConfig.contentLoading || false)
288+
LAST_COMMIT_HASH: JSON.stringify(getLastCommitHash())
290289
}])
291290

292291
pluginAPI.options.define.apply(config)

packages/@vuepress/core/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
"toml": "^2.3.3",
6161
"url-loader": "^1.0.1",
6262
"vue": "^2.5.16",
63-
"vue-content-loader": "^0.2.1",
6463
"vue-loader": "^15.2.4",
6564
"vue-router": "^3.0.1",
6665
"vue-server-renderer": "^2.5.16",

packages/@vuepress/plugin-active-header-links/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ module.exports = (options) => ({
44
clientRootMixin: path.resolve(__dirname, 'mixin.js'),
55
define: {
66
AHL_SIDEBAR_LINK_SELECTOR: options.sidebarLinkSelector || '.sidebar-link',
7-
AHL_HEADER_ANCHOR_SELECTOR: options.headerAnchorSelector || '.header-anchor',
8-
AHL_TOP_OFFSET: options.headerTopOffset || 90
7+
AHL_HEADER_ANCHOR_SELECTOR: options.headerAnchorSelector || '.header-anchor'
98
}
109
})

0 commit comments

Comments
 (0)