a short story about why using native APIs directly is probably evil
by
@MichalZalecki
ReactJS Wrocław, April 2017
Failed to register a ServiceWorker: The user denied permission to use Service Worker.
function storageFactory(source, prop) {
let inMemoryStorage = {};
function setItem(key, value) {
try {
getStorage().setItem(key, value);
} catch (e) {
inMemoryStorage[key] = value;
}
}
function getItem(key) {
try {
return getStorage().getItem(key);
} catch (e) {
return inMemoryStorage[key] || null;
}
}
return {
setItem,
getItem,
};
}
export const localStore = storageFactory(window, "localStorage");
export const sessionStore = storageFactory(window, "sessionStorage");