Replace fetch API with XMLHttpRequest to fetch local files ?
We are using Chrome Headless (with Puppeteer) to generate a PDF with Paged.js.
The Polisher
class is using fetch
to fetch the stylesheets: https://gitlab.pagedmedia.org/tools/pagedjs/blob/master/src/polisher/polisher.js#L52
Unfortunately fetch
native implementation in Chrome does not support fetching local files: https://github.com/github/fetch/pull/92#issuecomment-140665932
So even if we are explicitly disabling CORS, we still get an error:
fetch('file:///path/to/asciidoctor.css', {mode: 'no-cors'})
Fetch API cannot load file:///path/to/asciidoctor.css. URL scheme "file" is not supported.
If I declare the following function in Polisher
and replace fetch
with this.fetchLocal
then it's working:
async fetchLocal(url) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest
xhr.onload = function() {
resolve(new Response(xhr.responseText, {status: xhr.status}))
}
xhr.onerror = function() {
reject(new TypeError('Local request failed'))
}
xhr.open('GET', url)
xhr.send(null)
})
}
If you want to keep fetch
, would you consider an extension point so in our use case we can provide an implementation based on XMLHttpRequest
?
Thanks, Guillaume