diginext-utils
Version:
README.md
32 lines (31 loc) • 988 B
JavaScript
import getUrlBypassCors from "./getUrlBypassCors";
const tryLoad = async (url) => {
const response = await fetch(url, {
// mode: "cors",
cache: "force-cache",
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const imageBlob = await (response === null || response === void 0 ? void 0 : response.blob());
const blobUrl = URL.createObjectURL(imageBlob);
return blobUrl;
};
export default async function loadImageAsBlobUrl(imageUrl, addVerion = true) {
try {
const imageBlob = await tryLoad(imageUrl);
return imageBlob;
}
catch (error) { }
if (addVerion) {
try {
const url = getUrlBypassCors(imageUrl);
if (!url)
throw new Error("imageUrl error");
const imageBlob = await tryLoad(url);
return imageBlob;
}
catch (error) { }
}
return imageUrl;
}