Skip to content

Commit 02c611b

Browse files
committed
fix(fetch): load sidebar and navbar for parent path, fixed #100
1 parent 7fed449 commit 02c611b

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

src/core/fetch/index.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
import { get } from './ajax'
22
import { callHook } from '../init/lifecycle'
3-
import { getRoot } from '../route/util'
3+
import { getParentPath } from '../route/util'
44
import { noop } from '../util/core'
55

6+
function loadNested (path, file, next, vm, first) {
7+
path = first ? path : path.replace(/\/$/, '')
8+
path = getParentPath(path)
9+
10+
if (!path) return
11+
12+
get(vm.$getFile(path + file))
13+
.then(next, _ => loadNested(path, file, next, vm))
14+
}
15+
616
export function fetchMixin (proto) {
717
let last
818
proto._fetch = function (cb = noop) {
919
const { path } = this.route
1020
const { loadNavbar, loadSidebar } = this.config
11-
const root = getRoot(path)
1221

1322
// Abort last request
1423
last && last.abort && last.abort()
@@ -26,25 +35,18 @@ export function fetchMixin (proto) {
2635
const fn = result => { this._renderSidebar(result); cb() }
2736

2837
// Load sidebar
29-
get(this.$getFile(root + loadSidebar))
30-
// fallback root navbar when fail
31-
.then(fn, _ => get(loadSidebar).then(fn))
38+
loadNested(path, loadSidebar, fn, this, true)
3239
},
3340
_ => this._renderMain(null))
3441

3542
// Load nav
3643
loadNavbar &&
37-
get(this.$getFile(root + loadNavbar))
38-
.then(
39-
text => this._renderNav(text),
40-
// fallback root navbar when fail
41-
_ => get(loadNavbar).then(text => this._renderNav(text))
42-
)
44+
loadNested(path, loadNavbar, text => this._renderNav(text), this, true)
4345
}
4446

4547
proto._fetchCover = function () {
4648
const { coverpage } = this.config
47-
const root = getRoot(this.route.path)
49+
const root = getParentPath(this.route.path)
4850
const path = this.$getFile(root + coverpage)
4951

5052
if (this.route.path !== '/' || !coverpage) {

src/core/route/util.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@ export const isAbsolutePath = cached(path => {
4545
return /(:|(\/{2}))/.test(path)
4646
})
4747

48-
export const getRoot = cached(path => {
49-
return /\/$/g.test(path) ? path : path.match(/(\S*\/)[^\/]+$/)[1]
48+
export const getParentPath = cached(path => {
49+
return /\/$/g.test(path)
50+
? path
51+
: (path = path.match(/(\S*\/)[^\/]+$/))
52+
? path[1]
53+
: ''
5054
})
5155

5256
export const cleanPath = cached(path => {

0 commit comments

Comments
 (0)