Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

fix: XMLHttpRequest is deprecated and unavailable in service workers #1478

Merged
merged 1 commit into from
Jul 29, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 14 additions & 22 deletions src/core/runtime/preload-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,21 @@ log.error = debug('jsipfs:preload:error')
module.exports = function preload (url, callback) {
log(url)

const req = new self.XMLHttpRequest()

req.open('GET', url)

req.onreadystatechange = function () {
if (this.readyState !== this.DONE) {
return
}

if (this.status < 200 || this.status >= 300) {
log.error('failed to preload', url, this.status, this.statusText)
return callback(new Error(`failed to preload ${url}`))
}

callback()
}

req.send()
const controller = new AbortController()
const signal = controller.signal

fetch(url, { signal })
.then(res => {
if (!res.ok) {
log.error('failed to preload', url, res.status, res.statusText)
throw new Error(`failed to preload ${url}`)
}
return res.text()
})
.then(() => callback())
.catch(callback)

return {
cancel: () => {
req.abort()
callback(new Error('request aborted'))
}
cancel: () => controller.abort()
}
}