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 a cache for responses to fonts #336

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions dist/dom-to-image.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion src/dom-to-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
var fontFaces = newFontFaces();
var images = newImages();

var cacheForResponses = [];

// Default impl options
var defaultOptions = {
// Default is to fail on error, no placeholder
Expand Down Expand Up @@ -462,13 +464,19 @@

function getAndEncode(url) {
var TIMEOUT = 30000;
var _url = url.split('?')[0];
var _obj = cacheForResponses.find(function (el) {
return el.url === _url;
});
if (_obj) return _obj.promise;

if(domtoimage.impl.options.cacheBust) {
// 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();
}

return new Promise(function (resolve) {
var promise = new Promise(function (resolve) {
var request = new XMLHttpRequest();

request.onreadystatechange = done;
Expand Down Expand Up @@ -520,6 +528,12 @@
resolve('');
}
});
_obj = {
url: _url,
promise: promise
};
cacheForResponses.push(_obj);
return _obj.promise;
}

function dataAsUrl(content, type) {
Expand Down