Skip to content

Commit 54ab4c9

Browse files
committed
feat(fetch): add requestHeaders option, fixed #336
1 parent f960c19 commit 54ab4c9

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

src/core/fetch/ajax.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const cache = {}
99
* @param {boolean} [hasBar=false] has progress bar
1010
* @return { then(resolve, reject), abort }
1111
*/
12-
export function get (url, hasBar = false) {
12+
export function get (url, hasBar = false, headers = {}) {
1313
const xhr = new XMLHttpRequest()
1414
const on = function () {
1515
xhr.addEventListener.apply(xhr, arguments)
@@ -21,6 +21,9 @@ export function get (url, hasBar = false) {
2121
}
2222

2323
xhr.open('GET', url)
24+
for (const i in headers) {
25+
xhr.setRequestHeader(i, headers[i])
26+
}
2427
xhr.send()
2528

2629
return {

src/core/fetch/index.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,24 @@ function loadNested (path, qs, file, next, vm, first) {
1010

1111
if (!path) return
1212

13-
get(vm.router.getFile(path + file) + qs).then(next, _ =>
14-
loadNested(path, qs, file, next, vm)
15-
)
13+
get(
14+
vm.router.getFile(path + file) + qs,
15+
false,
16+
vm.config.requestHeaders
17+
).then(next, _ => loadNested(path, qs, file, next, vm))
1618
}
1719

1820
export function fetchMixin (proto) {
1921
let last
2022
proto._fetch = function (cb = noop) {
2123
const { path, query } = this.route
2224
const qs = stringifyQuery(query, ['id'])
23-
const { loadNavbar, loadSidebar } = this.config
25+
const { loadNavbar, loadSidebar, requestHeaders } = this.config
2426

2527
// Abort last request
2628
last && last.abort && last.abort()
2729

28-
last = get(this.router.getFile(path) + qs, true)
30+
last = get(this.router.getFile(path) + qs, true, requestHeaders)
2931

3032
// Current page is html
3133
this.isHTML = /\.html$/g.test(path)
@@ -67,7 +69,7 @@ export function fetchMixin (proto) {
6769
}
6870

6971
proto._fetchCover = function () {
70-
const { coverpage } = this.config
72+
const { coverpage, requestHeaders } = this.config
7173
const query = this.route.query
7274
const root = getParentPath(this.route.path)
7375

@@ -89,8 +91,8 @@ export function fetchMixin (proto) {
8991
if (path) {
9092
path = this.router.getFile(root + path)
9193
this.coverIsHTML = /\.html$/g.test(path)
92-
get(path + stringifyQuery(query, ['id'])).then(text =>
93-
this._renderCover(text)
94+
get(path + stringifyQuery(query, ['id']), false, requestHeaders).then(
95+
text => this._renderCover(text)
9496
)
9597
} else {
9698
this._renderCover()

src/plugins/search/search.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,11 @@ export function init (config, vm) {
165165
paths.forEach(path => {
166166
if (INDEXS[path]) return count++
167167

168-
helper.get(vm.router.getFile(path)).then(result => {
169-
INDEXS[path] = genIndex(path, result, vm.router, config.depth)
170-
len === ++count && saveData(config.maxAge)
171-
})
168+
helper
169+
.get(vm.router.getFile(path), false, vm.config.requestHeaders)
170+
.then(result => {
171+
INDEXS[path] = genIndex(path, result, vm.router, config.depth)
172+
len === ++count && saveData(config.maxAge)
173+
})
172174
})
173175
}

0 commit comments

Comments
 (0)