Skip to content

Commit

Permalink
Fallback to localstorage is indexedDb not available, refs #13, #10
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamjain committed Jun 24, 2022
1 parent cb882dd commit 0076bf5
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions svg-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,47 @@ const cssUrlFixer = require("./lib/css-url-fixer");
const counter = require("./lib/counter");

const isCacheAvailable = async (url) => {
let item;

try {
let item = await get(`loader_${url}`);
item = await get(`loader_${url}`);
} catch (e) {}

if (!item) {
return;
}
if (!item) {
try {
item = localStorage.getItem(`loader_${url}`);
} catch(e) {}
}

item = JSON.parse(item);
if (!item) {
return;
}

if (Date.now() < item.expiry) {
return item.data;
} else {
del(`loader_${url}`);
return;
}
} catch (e) {
item = JSON.parse(item);

if (Date.now() < item.expiry) {
return item.data;
} else {
del(`loader_${url}`);
return;
}
};

const setCache = async (url, data, cacheOpt) => {
try {
const cacheExp = parseInt(cacheOpt, 10);

await set(`loader_${url}`, JSON.stringify({
data,
expiry: Date.now() + (Number.isNaN(cacheExp) ? 60 * 60 * 1000 * 24 : cacheExp)
}));
const cacheExp = parseInt(cacheOpt, 10);
const dataToSet = JSON.stringify({
data,
expiry: Date.now() + (Number.isNaN(cacheExp) ? 60 * 60 * 1000 * 24 : cacheExp)
});

try {
await set(`loader_${url}`, dataToSet);
} catch (e) {
console.error(e);
try {
localStorage.setItem(`loader_${url}`, dataToSet)
} catch (e) {
console.warn("Failed to set cache: ", e)
}
};
};

Expand Down

0 comments on commit 0076bf5

Please sign in to comment.