Skip to content

Commit

Permalink
Fix transparent video on safari 14.1 by using fetch instead of XHR (#282
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Nir Maoz authored Jan 6, 2022
1 parent e97d198 commit 7654e56
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 17 deletions.
2 changes: 1 addition & 1 deletion bundlewatch.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const bundlewatchConfig = {
},
{
path: './dist/cloudinary-core-shrinkwrap.min.js',
maxSize: '31kb'
maxSize: '32kb'
},
{
path: './dist/cloudinary-jquery.min.js',
Expand Down
80 changes: 64 additions & 16 deletions src/util/xhr/getBlobFromURL.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/**
* Reject on timeout
* @param maxTimeoutMS
* @param reject
* @returns {number} timerID
*/
function rejectOnTimeout(maxTimeoutMS, reject) {
return setTimeout(() => {
reject({
status: 'error',
message: 'Timeout loading Blob URL'
});
}, maxTimeoutMS);
}

/**
* @description Converts a URL to a BLOB URL
* @param {string} urlToLoad
Expand All @@ -10,34 +25,67 @@
* }
* }>}
*/
function getBlobFromURL(urlToLoad, max_timeout_ms) {
function getBlobFromURL(urlToLoad, maxTimeoutMS) {
return new Promise((resolve, reject) => {
let timerID = setTimeout(() => {
reject({
status: 'error',
message: 'Timeout loading Blob URL'
});
}, max_timeout_ms);
const timerID = rejectOnTimeout(maxTimeoutMS, reject);

let xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function (response) {
clearTimeout(timerID); // clear timeout reject error
// If fetch exists, use it to fetch blob, otherwise use XHR.
// XHR causes issues on safari 14.1 so we prefer fetch
const fetchBlob = (typeof fetch !== 'undefined' && fetch) ? loadUrlUsingFetch : loadUrlUsingXhr;

fetchBlob(urlToLoad).then((blob) => {
resolve({
status: 'success',
payload: {
blobURL: URL.createObjectURL(xhr.response)
blobURL: URL.createObjectURL(blob)
}
});
};

xhr.onerror = function () {
clearTimeout(timerID); // clear timeout reject error
}).catch(() => {
reject({
status: 'error',
message: 'Error loading Blob URL'
});
}).finally(() => {
// Clear the timeout timer on fail or success.
clearTimeout(timerID);
});
});
}

/**
* Use fetch function to fetch file
* @param urlToLoad
* @returns {Promise<unknown>}
*/
function loadUrlUsingFetch(urlToLoad) {
return new Promise((resolve, reject) => {
fetch(urlToLoad).then((response) => {
response.blob().then((blob) => {
resolve(blob);
});
}).catch(() => {
reject('error');
});
});
}

/**
* Use XHR to fetch file
* @param urlToLoad
* @returns {Promise<unknown>}
*/
function loadUrlUsingXhr(urlToLoad) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function (response) {
resolve(xhr.response);
};

xhr.onerror = function () {
reject('error');
};

xhr.open('GET', urlToLoad, true);
xhr.send();
});
Expand Down

0 comments on commit 7654e56

Please sign in to comment.