preload.io-raw
v1.0.0
Published
Preload.io loader for returning responses
Downloads
4
Readme
preload.io-raw
Preload.io loader to expose the raw response body
Installation
npm i preload.io-raw
Polyfills
The loader requires a few polyfills to work everywhere, to give some flexibility they are not included by default.
npm i -S whatwg-fetch regenerator
import 'regenerator/runtime'
import 'whatwg-fetch'
Regenerator
is currently a requirement for the async stuff, but a version is included with babel-polyfill
so if you’re using that then you’re good to go. Use whichever version of fetch
you like, if necessary.
There will be a fairly obvious console error logged if these are omitted.
Getting Started
Install preload.io and register the raw loader with it
import Preloader from 'preload.io'
import { EVENTS } from 'preload.io'
import RawLoader from 'preload.io-raw'
let preloader = new Preloader()
preloader.register( new RawLoader() )
Then just load the resource and listen for the response
let id = preloader.load( 'https://example.com/example.txt' )
preloader.on( EVENTS.LOAD, event => {
if ( event.id === id ) {
console.log( event.res )
}
})
Passing options to fetch
RawLoader
uses fetch
to preload the resource, options you supply will be passed to fetch
, in this order of precedence:
preloader.load({
resource: 'stuff.txt',
options: {
mode: 'no-cors'
}
})
const rawLoader = new RawLoader({
mode: 'no-cors'
})
const preloader = new Preloader({
mode: 'no-cors'
})