" />

Better loading with PreloadJS: withCredentials and headers

PreloadJS has always been treated as a “load anything” library – allowing you to preload any file type with minimal code. But due to the nature of the internet, sometimes requests need to be customized, or security flags set. To allow for lower level request customization, we added two new parameters on load items: withCredentials and headers.

withCredentials:

Server side applications commonly send cookies with authorization data included. Generally browsers will automatically send this data back with requests. The idea being that client side developers have no knowledge of these credentials. When making XmlHttpRequests, devs need to tell XHR objects to send these values along with the current request. This is where withCredentials comes in. When you make a request you can set {withCredentials: true}, and PreloadJS will pass along this value so authenticated requests work correctly.

var loadItem = {withCredentials:true, src:”http://file.com/api.php”, data:{foo:bar, baz:1}, method:createjs.LoadQueue.POST};
var request = new createjs.LoadQueue(loadItem);
request.loadFile(loadItem);

headers:

Preload has had internal support for setting required headers in the background for a while, but now you can pass a headers object to any load item using the headers parameter. Any default headers will be overridden with custom ones that are passed in:

var loadItem = new createjs.LoadItem().set({
src: “http://path/to/image.png”,
headers: {"X-My-Header":"foo", "Content-Type": "application/x-www-form-urlencoded"}
});

This change is made easier with the new LoadItem class (shown above), which can optionally be used in place of un-typed objects. Adding this class creates a much more official way to set up loads, and also made documentation a lot easier. Feel free to continue to use un-typed objects, or string paths – your existing code will not be affected.

These updates are part of the upcoming release on December 12th, and are currently part of the ExtendReArch branch in GitHub, and should be merged to the master branch in the next few days.

Leave a Reply

Your email address will not be published. Required fields are marked *

© Copyright 2017