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

Commit

Permalink
fix: XMLHTTPRequest is deprecated and unavailable in service workers (#…
Browse files Browse the repository at this point in the history
…1478)

This PR switches to using the fetch API instead.

License: MIT
Signed-off-by: Alan Shaw <alan@tableflip.io>
  • Loading branch information
alanshaw authored Jul 29, 2018
1 parent a9219ad commit 7d6f0ca
Showing 1 changed file with 14 additions and 22 deletions.
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()
}
}

0 comments on commit 7d6f0ca

Please sign in to comment.