diff --git a/src/core/fetch/index.js b/src/core/fetch/index.js index 61577ac..0f2380a 100644 --- a/src/core/fetch/index.js +++ b/src/core/fetch/index.js @@ -1,14 +1,23 @@ import { get } from './ajax' import { callHook } from '../init/lifecycle' -import { getRoot } from '../route/util' +import { getParentPath } from '../route/util' import { noop } from '../util/core' +function loadNested (path, file, next, vm, first) { + path = first ? path : path.replace(/\/$/, '') + path = getParentPath(path) + + if (!path) return + + get(vm.$getFile(path + file)) + .then(next, _ => loadNested(path, file, next, vm)) +} + export function fetchMixin (proto) { let last proto._fetch = function (cb = noop) { const { path } = this.route const { loadNavbar, loadSidebar } = this.config - const root = getRoot(path) // Abort last request last && last.abort && last.abort() @@ -26,25 +35,18 @@ export function fetchMixin (proto) { const fn = result => { this._renderSidebar(result); cb() } // Load sidebar - get(this.$getFile(root + loadSidebar)) - // fallback root navbar when fail - .then(fn, _ => get(loadSidebar).then(fn)) + loadNested(path, loadSidebar, fn, this, true) }, _ => this._renderMain(null)) // Load nav loadNavbar && - get(this.$getFile(root + loadNavbar)) - .then( - text => this._renderNav(text), - // fallback root navbar when fail - _ => get(loadNavbar).then(text => this._renderNav(text)) - ) + loadNested(path, loadNavbar, text => this._renderNav(text), this, true) } proto._fetchCover = function () { const { coverpage } = this.config - const root = getRoot(this.route.path) + const root = getParentPath(this.route.path) const path = this.$getFile(root + coverpage) if (this.route.path !== '/' || !coverpage) { diff --git a/src/core/route/util.js b/src/core/route/util.js index 3f742c8..cfdd2a9 100644 --- a/src/core/route/util.js +++ b/src/core/route/util.js @@ -45,8 +45,12 @@ export const isAbsolutePath = cached(path => { return /(:|(\/{2}))/.test(path) }) -export const getRoot = cached(path => { - return /\/$/g.test(path) ? path : path.match(/(\S*\/)[^\/]+$/)[1] +export const getParentPath = cached(path => { + return /\/$/g.test(path) + ? path + : (path = path.match(/(\S*\/)[^\/]+$/)) + ? path[1] + : '' }) export const cleanPath = cached(path => {