From 54ab4c9ff73f861cc6ef8bbdca4bb1395234382c Mon Sep 17 00:00:00 2001 From: "qingwei.li" Date: Sat, 10 Feb 2018 17:31:26 +0800 Subject: [PATCH] feat(fetch): add requestHeaders option, fixed #336 --- src/core/fetch/ajax.js | 5 ++++- src/core/fetch/index.js | 18 ++++++++++-------- src/plugins/search/search.js | 10 ++++++---- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/core/fetch/ajax.js b/src/core/fetch/ajax.js index 28d66444d..d51323916 100644 --- a/src/core/fetch/ajax.js +++ b/src/core/fetch/ajax.js @@ -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) @@ -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 { diff --git a/src/core/fetch/index.js b/src/core/fetch/index.js index 16f396923..045089864 100644 --- a/src/core/fetch/index.js +++ b/src/core/fetch/index.js @@ -10,9 +10,11 @@ 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) { @@ -20,12 +22,12 @@ export function fetchMixin (proto) { 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) @@ -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) @@ -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() diff --git a/src/plugins/search/search.js b/src/plugins/search/search.js index 4c73e2def..6a440c278 100644 --- a/src/plugins/search/search.js +++ b/src/plugins/search/search.js @@ -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) + }) }) }