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

Commit 7d6f0ca

Browse files
authored
fix: XMLHTTPRequest is deprecated and unavailable in service workers (#1478)
This PR switches to using the fetch API instead. License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent a9219ad commit 7d6f0ca

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

src/core/runtime/preload-browser.js

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,21 @@ log.error = debug('jsipfs:preload:error')
99
module.exports = function preload (url, callback) {
1010
log(url)
1111

12-
const req = new self.XMLHttpRequest()
13-
14-
req.open('GET', url)
15-
16-
req.onreadystatechange = function () {
17-
if (this.readyState !== this.DONE) {
18-
return
19-
}
20-
21-
if (this.status < 200 || this.status >= 300) {
22-
log.error('failed to preload', url, this.status, this.statusText)
23-
return callback(new Error(`failed to preload ${url}`))
24-
}
25-
26-
callback()
27-
}
28-
29-
req.send()
12+
const controller = new AbortController()
13+
const signal = controller.signal
14+
15+
fetch(url, { signal })
16+
.then(res => {
17+
if (!res.ok) {
18+
log.error('failed to preload', url, res.status, res.statusText)
19+
throw new Error(`failed to preload ${url}`)
20+
}
21+
return res.text()
22+
})
23+
.then(() => callback())
24+
.catch(callback)
3025

3126
return {
32-
cancel: () => {
33-
req.abort()
34-
callback(new Error('request aborted'))
35-
}
27+
cancel: () => controller.abort()
3628
}
3729
}

0 commit comments

Comments
 (0)