Skip to content

Commit

Permalink
feat(fetch): add requestHeaders option, fixed #336
Browse files Browse the repository at this point in the history
  • Loading branch information
QingWei-Li committed Feb 11, 2018
1 parent f960c19 commit 54ab4c9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
5 changes: 4 additions & 1 deletion src/core/fetch/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const cache = {}
* @param {boolean} [hasBar=false] has progress bar
* @return { then(resolve, reject), abort }
*/
export function get (url, hasBar = false) {
export function get (url, hasBar = false, headers = {}) {
const xhr = new XMLHttpRequest()
const on = function () {
xhr.addEventListener.apply(xhr, arguments)
Expand All @@ -21,6 +21,9 @@ export function get (url, hasBar = false) {
}

xhr.open('GET', url)
for (const i in headers) {
xhr.setRequestHeader(i, headers[i])
}
xhr.send()

return {
Expand Down
18 changes: 10 additions & 8 deletions src/core/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,24 @@ function loadNested (path, qs, file, next, vm, first) {

if (!path) return

get(vm.router.getFile(path + file) + qs).then(next, _ =>
loadNested(path, qs, file, next, vm)
)
get(
vm.router.getFile(path + file) + qs,
false,
vm.config.requestHeaders
).then(next, _ => loadNested(path, qs, file, next, vm))
}

export function fetchMixin (proto) {
let last
proto._fetch = function (cb = noop) {
const { path, query } = this.route
const qs = stringifyQuery(query, ['id'])
const { loadNavbar, loadSidebar } = this.config
const { loadNavbar, loadSidebar, requestHeaders } = this.config

// Abort last request
last && last.abort && last.abort()

last = get(this.router.getFile(path) + qs, true)
last = get(this.router.getFile(path) + qs, true, requestHeaders)

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

proto._fetchCover = function () {
const { coverpage } = this.config
const { coverpage, requestHeaders } = this.config
const query = this.route.query
const root = getParentPath(this.route.path)

Expand All @@ -89,8 +91,8 @@ export function fetchMixin (proto) {
if (path) {
path = this.router.getFile(root + path)
this.coverIsHTML = /\.html$/g.test(path)
get(path + stringifyQuery(query, ['id'])).then(text =>
this._renderCover(text)
get(path + stringifyQuery(query, ['id']), false, requestHeaders).then(
text => this._renderCover(text)
)
} else {
this._renderCover()
Expand Down
10 changes: 6 additions & 4 deletions src/plugins/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,11 @@ export function init (config, vm) {
paths.forEach(path => {
if (INDEXS[path]) return count++

helper.get(vm.router.getFile(path)).then(result => {
INDEXS[path] = genIndex(path, result, vm.router, config.depth)
len === ++count && saveData(config.maxAge)
})
helper
.get(vm.router.getFile(path), false, vm.config.requestHeaders)
.then(result => {
INDEXS[path] = genIndex(path, result, vm.router, config.depth)
len === ++count && saveData(config.maxAge)
})
})
}

0 comments on commit 54ab4c9

Please sign in to comment.