mirror of
https://github.com/docsifyjs/docsify.git
synced 2025-12-08 19:55:52 +00:00
feat(fetch): add requestHeaders option, fixed #336
This commit is contained in:
parent
f960c196bf
commit
54ab4c9ff7
@ -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 {
|
||||
|
||||
@ -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()
|
||||
|
||||
@ -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)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user