Skip to content

Commit

Permalink
Validate localStorage and sessionStorage only if I use these resources (
Browse files Browse the repository at this point in the history
#222)

* Validate localStorage and sessionStorage support only if I use these resources/features

* Checking sessionStorage and localStorage support only once

* Reducing redundant code for sessionStorage and localStorage support be called only once
  • Loading branch information
robertymb committed Aug 18, 2020
1 parent e697ea8 commit 90284ca
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
26 changes: 16 additions & 10 deletions src/browserLookups/localStorage.js
@@ -1,11 +1,17 @@
let hasLocalStorageSupport;
try {
hasLocalStorageSupport = window !== 'undefined' && window.localStorage !== null;
const testKey = 'i18next.translate.boo';
window.localStorage.setItem(testKey, 'foo');
window.localStorage.removeItem(testKey);
} catch (e) {
hasLocalStorageSupport = false;
let hasLocalStorageSupport = null;

const localStorageAvailable = () => {
if (hasLocalStorageSupport !== null) return hasLocalStorageSupport;

try {
hasLocalStorageSupport = window !== 'undefined' && window.localStorage !== null;
const testKey = 'i18next.translate.boo';
window.localStorage.setItem(testKey, 'foo');
window.localStorage.removeItem(testKey);
} catch (e) {
hasLocalStorageSupport = false;
}
return hasLocalStorageSupport;
}

export default {
Expand All @@ -14,7 +20,7 @@ export default {
lookup(options) {
let found;

if (options.lookupLocalStorage && hasLocalStorageSupport) {
if (options.lookupLocalStorage && localStorageAvailable()) {
const lng = window.localStorage.getItem(options.lookupLocalStorage);
if (lng) found = lng;
}
Expand All @@ -23,7 +29,7 @@ export default {
},

cacheUserLanguage(lng, options) {
if (options.lookupLocalStorage && hasLocalStorageSupport) {
if (options.lookupLocalStorage && localStorageAvailable()) {
window.localStorage.setItem(options.lookupLocalStorage, lng);
}
}
Expand Down
26 changes: 16 additions & 10 deletions src/browserLookups/sessionStorage.js
@@ -1,11 +1,17 @@
let hasSessionStorageSupport;
try {
hasSessionStorageSupport = window !== 'undefined' && window.sessionStorage !== null;
const testKey = 'i18next.translate.boo';
window.sessionStorage.setItem(testKey, 'foo');
window.sessionStorage.removeItem(testKey);
} catch (e) {
hasSessionStorageSupport = false;
let hasSessionStorageSupport = null;

const sessionStorageAvailable = () => {
if (hasSessionStorageSupport !== null) return hasSessionStorageSupport;

try {
hasSessionStorageSupport = window !== 'undefined' && window.sessionStorage !== null;
const testKey = 'i18next.translate.boo';
window.sessionStorage.setItem(testKey, 'foo');
window.sessionStorage.removeItem(testKey);
} catch (e) {
hasSessionStorageSupport = false;
}
return hasSessionStorageSupport;
}

export default {
Expand All @@ -14,7 +20,7 @@ export default {
lookup(options) {
let found;

if (options.lookupSessionStorage && hasSessionStorageSupport) {
if (options.lookupSessionStorage && sessionStorageAvailable()) {
const lng = window.sessionStorage.getItem(options.lookupSessionStorage);
if (lng) found = lng;
}
Expand All @@ -23,7 +29,7 @@ export default {
},

cacheUserLanguage(lng, options) {
if (options.lookupSessionStorage && hasSessionStorageSupport) {
if (options.lookupSessionStorage && sessionStorageAvailable()) {
window.sessionStorage.setItem(options.lookupSessionStorage, lng);
}
}
Expand Down

0 comments on commit 90284ca

Please sign in to comment.