Skip to content

Commit 2a2ed96

Browse files
committed
feat: fetch files with the query params, fixed #303
1 parent 0d54c0c commit 2a2ed96

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

Diff for: src/core/fetch/index.js

+35-19
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,74 @@
11
import { get } from './ajax'
22
import { callHook } from '../init/lifecycle'
3-
import { getParentPath } from '../router/util'
3+
import { getParentPath, stringifyQuery } from '../router/util'
44
import { noop } from '../util/core'
55
import { getAndActive } from '../event/sidebar'
66

7-
function loadNested (path, file, next, vm, first) {
7+
function loadNested (path, qs, file, next, vm, first) {
88
path = first ? path : path.replace(/\/$/, '')
99
path = getParentPath(path)
1010

1111
if (!path) return
1212

13-
get(vm.router.getFile(path + file))
14-
.then(next, _ => loadNested(path, file, next, vm))
13+
get(vm.router.getFile(path + file) + qs).then(next, _ =>
14+
loadNested(path, qs, file, next, vm)
15+
)
1516
}
1617

1718
export function fetchMixin (proto) {
1819
let last
1920
proto._fetch = function (cb = noop) {
20-
const { path } = this.route
21+
const { path, query } = this.route
22+
const qs = stringifyQuery(query, ['id'])
2123
const { loadNavbar, loadSidebar } = this.config
2224

2325
// Abort last request
2426
last && last.abort && last.abort()
2527

26-
last = get(this.router.getFile(path), true)
28+
last = get(this.router.getFile(path) + qs, true)
2729

2830
// Current page is html
2931
this.isHTML = /\.html$/g.test(path)
3032

3133
const loadSideAndNav = () => {
3234
if (!loadSidebar) return cb()
3335

34-
const fn = result => { this._renderSidebar(result); cb() }
36+
const fn = result => {
37+
this._renderSidebar(result)
38+
cb()
39+
}
3540

3641
// Load sidebar
37-
loadNested(path, loadSidebar, fn, this, true)
42+
loadNested(path, qs, loadSidebar, fn, this, true)
3843
}
3944

4045
// Load main content
41-
last.then((text, opt) => {
42-
this._renderMain(text, opt)
43-
loadSideAndNav()
44-
},
45-
_ => {
46-
this._renderMain(null)
47-
loadSideAndNav()
48-
})
46+
last.then(
47+
(text, opt) => {
48+
this._renderMain(text, opt)
49+
loadSideAndNav()
50+
},
51+
_ => {
52+
this._renderMain(null)
53+
loadSideAndNav()
54+
}
55+
)
4956

5057
// Load nav
5158
loadNavbar &&
52-
loadNested(path, loadNavbar, text => this._renderNav(text), this, true)
59+
loadNested(
60+
path,
61+
qs,
62+
loadNavbar,
63+
text => this._renderNav(text),
64+
this,
65+
true
66+
)
5367
}
5468

5569
proto._fetchCover = function () {
5670
const { coverpage } = this.config
71+
const query = this.route.query
5772
const root = getParentPath(this.route.path)
5873
const path = this.router.getFile(root + coverpage)
5974

@@ -63,8 +78,9 @@ export function fetchMixin (proto) {
6378
}
6479

6580
this.coverIsHTML = /\.html$/g.test(path)
66-
get(path)
67-
.then(text => this._renderCover(text))
81+
get(path + stringifyQuery(query, ['id'])).then(text =>
82+
this._renderCover(text)
83+
)
6884
}
6985

7086
proto.$fetch = function (cb = noop) {

Diff for: src/core/router/util.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ export function parseQuery (query) {
2222
return res
2323
}
2424

25-
export function stringifyQuery (obj) {
25+
export function stringifyQuery (obj, ignores = []) {
2626
const qs = []
2727

2828
for (const key in obj) {
29+
if (ignores.indexOf(key) > -1) continue
2930
qs.push(
3031
obj[key]
3132
? `${encode(key)}=${encode(obj[key])}`.toLowerCase()

0 commit comments

Comments
 (0)