Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new option to set a custom timeout #358

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions src/dom-to-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
var defaultOptions = {
// Default is to fail on error, no placeholder
imagePlaceholder: undefined,
// Default cache bust is false, it will use the cache
cacheBust: false
// Default hard cache bust is false, it will use the cache
hardCacheBust: false,
// Default cache bust is false, it will use the cache
cacheBust: false,
// Default timeout
timeout: 30000
};

var domtoimage = {
Expand Down Expand Up @@ -47,7 +51,9 @@
* @param {Number} options.quality - a Number between 0 and 1 indicating image quality (applicable to JPEG only),
defaults to 1.0.
* @param {String} options.imagePlaceholder - dataURL to use as a placeholder for failed images, default behaviour is to fail fast on images we can't fetch
* @param {Boolean} options.cacheBust - set to true to cache bust by appending the time to the request url
* @param {Boolean} options.hardCacheBust - set to true to cache bust by appending the time to the request url
* @param {Boolean} options.cacheBust - set to true to cache bust by adding Cache-Control to the header
* @param {Boolean} options.timeout - set to true to set a timeout
* @return {Promise} - A promise that is fulfilled with a SVG image data URL
* */
function toSvg(node, options) {
Expand Down Expand Up @@ -142,11 +148,24 @@
domtoimage.impl.options.imagePlaceholder = options.imagePlaceholder;
}

if(typeof options.hardCacheBust === 'undefined') {
domtoimage.impl.options.hardCacheBust = defaultOptions.hardCacheBust;
} else {
domtoimage.impl.options.hardCacheBust = options.hardCacheBust;
}


if(typeof(options.cacheBust) === 'undefined') {
domtoimage.impl.options.cacheBust = defaultOptions.cacheBust;
} else {
domtoimage.impl.options.cacheBust = options.cacheBust;
}

if(typeof(options.timeout) === 'undefined') {
domtoimage.impl.options.timeout = defaultOptions.timeout;
} else {
domtoimage.impl.options.timeout = options.timeout;
}
}

function draw(domNode, options) {
Expand Down Expand Up @@ -461,8 +480,8 @@
}

function getAndEncode(url) {
var TIMEOUT = 30000;
if(domtoimage.impl.options.cacheBust) {
var TIMEOUT = domtoimage.impl.options.timeout;
if(domtoimage.impl.options.hardCacheBust) {
// Cache bypass so we dont have CORS issues with cached images
// Source: https://developer.mozilla.org/en/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Bypassing_the_cache
url += ((/\?/).test(url) ? "&" : "?") + (new Date()).getTime();
Expand All @@ -476,6 +495,13 @@
request.responseType = 'blob';
request.timeout = TIMEOUT;
request.open('GET', url, true);

if(domtoimage.impl.options.cacheBust || domtoimage.impl.options.hardCacheBust) {
request.setRequestHeader("Cache-Control", "no-cache, no-store, max-age=0");
request.setRequestHeader("Expires", "Tue, 01 Jan 1980 1:00:00 GMT");
request.setRequestHeader("Pragma", "no-cache");
}

request.send();

var placeholder;
Expand Down