feat(fetch): add requestHeaders option, fixed #336
This commit is contained in:
parent
f960c196bf
commit
54ab4c9ff7
3 changed files with 20 additions and 13 deletions
|
|
@ -9,7 +9,7 @@ const cache = {}
|
||||||
* @param {boolean} [hasBar=false] has progress bar
|
* @param {boolean} [hasBar=false] has progress bar
|
||||||
* @return { then(resolve, reject), abort }
|
* @return { then(resolve, reject), abort }
|
||||||
*/
|
*/
|
||||||
export function get (url, hasBar = false) {
|
export function get (url, hasBar = false, headers = {}) {
|
||||||
const xhr = new XMLHttpRequest()
|
const xhr = new XMLHttpRequest()
|
||||||
const on = function () {
|
const on = function () {
|
||||||
xhr.addEventListener.apply(xhr, arguments)
|
xhr.addEventListener.apply(xhr, arguments)
|
||||||
|
|
@ -21,6 +21,9 @@ export function get (url, hasBar = false) {
|
||||||
}
|
}
|
||||||
|
|
||||||
xhr.open('GET', url)
|
xhr.open('GET', url)
|
||||||
|
for (const i in headers) {
|
||||||
|
xhr.setRequestHeader(i, headers[i])
|
||||||
|
}
|
||||||
xhr.send()
|
xhr.send()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
|
|
@ -10,9 +10,11 @@ function loadNested (path, qs, file, next, vm, first) {
|
||||||
|
|
||||||
if (!path) return
|
if (!path) return
|
||||||
|
|
||||||
get(vm.router.getFile(path + file) + qs).then(next, _ =>
|
get(
|
||||||
loadNested(path, qs, file, next, vm)
|
vm.router.getFile(path + file) + qs,
|
||||||
)
|
false,
|
||||||
|
vm.config.requestHeaders
|
||||||
|
).then(next, _ => loadNested(path, qs, file, next, vm))
|
||||||
}
|
}
|
||||||
|
|
||||||
export function fetchMixin (proto) {
|
export function fetchMixin (proto) {
|
||||||
|
|
@ -20,12 +22,12 @@ export function fetchMixin (proto) {
|
||||||
proto._fetch = function (cb = noop) {
|
proto._fetch = function (cb = noop) {
|
||||||
const { path, query } = this.route
|
const { path, query } = this.route
|
||||||
const qs = stringifyQuery(query, ['id'])
|
const qs = stringifyQuery(query, ['id'])
|
||||||
const { loadNavbar, loadSidebar } = this.config
|
const { loadNavbar, loadSidebar, requestHeaders } = this.config
|
||||||
|
|
||||||
// Abort last request
|
// Abort last request
|
||||||
last && last.abort && last.abort()
|
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
|
// Current page is html
|
||||||
this.isHTML = /\.html$/g.test(path)
|
this.isHTML = /\.html$/g.test(path)
|
||||||
|
|
@ -67,7 +69,7 @@ export function fetchMixin (proto) {
|
||||||
}
|
}
|
||||||
|
|
||||||
proto._fetchCover = function () {
|
proto._fetchCover = function () {
|
||||||
const { coverpage } = this.config
|
const { coverpage, requestHeaders } = this.config
|
||||||
const query = this.route.query
|
const query = this.route.query
|
||||||
const root = getParentPath(this.route.path)
|
const root = getParentPath(this.route.path)
|
||||||
|
|
||||||
|
|
@ -89,8 +91,8 @@ export function fetchMixin (proto) {
|
||||||
if (path) {
|
if (path) {
|
||||||
path = this.router.getFile(root + path)
|
path = this.router.getFile(root + path)
|
||||||
this.coverIsHTML = /\.html$/g.test(path)
|
this.coverIsHTML = /\.html$/g.test(path)
|
||||||
get(path + stringifyQuery(query, ['id'])).then(text =>
|
get(path + stringifyQuery(query, ['id']), false, requestHeaders).then(
|
||||||
this._renderCover(text)
|
text => this._renderCover(text)
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
this._renderCover()
|
this._renderCover()
|
||||||
|
|
|
||||||
|
|
@ -165,9 +165,11 @@ export function init (config, vm) {
|
||||||
paths.forEach(path => {
|
paths.forEach(path => {
|
||||||
if (INDEXS[path]) return count++
|
if (INDEXS[path]) return count++
|
||||||
|
|
||||||
helper.get(vm.router.getFile(path)).then(result => {
|
helper
|
||||||
INDEXS[path] = genIndex(path, result, vm.router, config.depth)
|
.get(vm.router.getFile(path), false, vm.config.requestHeaders)
|
||||||
len === ++count && saveData(config.maxAge)
|
.then(result => {
|
||||||
})
|
INDEXS[path] = genIndex(path, result, vm.router, config.depth)
|
||||||
|
len === ++count && saveData(config.maxAge)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue