Skip to content

Commit c24af00

Browse files
authored
fix detection of window existence (#300)
* fix detection of window existence * reverse flag/storage check
1 parent c7ba075 commit c24af00

4 files changed

+19
-9
lines changed

i18nextBrowserLanguageDetector.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,10 @@
162162
const localStorageAvailable = () => {
163163
if (hasLocalStorageSupport !== null) return hasLocalStorageSupport;
164164
try {
165-
hasLocalStorageSupport = window !== 'undefined' && window.localStorage !== null;
165+
hasLocalStorageSupport = typeof window !== 'undefined' && window.localStorage !== null;
166+
if (!hasLocalStorageSupport) {
167+
return false;
168+
}
166169
const testKey = 'i18next.translate.boo';
167170
window.localStorage.setItem(testKey, 'foo');
168171
window.localStorage.removeItem(testKey);
@@ -178,17 +181,18 @@
178181
let {
179182
lookupLocalStorage
180183
} = _ref;
181-
if (localStorageAvailable() && lookupLocalStorage) {
184+
if (lookupLocalStorage && localStorageAvailable()) {
182185
return window.localStorage.getItem(lookupLocalStorage) || undefined; // Undefined ensures type consistency with the previous version of this function
183186
}
187+
184188
return undefined;
185189
},
186190
// Deconstruct the options object and extract the lookupLocalStorage property
187191
cacheUserLanguage(lng, _ref2) {
188192
let {
189193
lookupLocalStorage
190194
} = _ref2;
191-
if (localStorageAvailable() && lookupLocalStorage) {
195+
if (lookupLocalStorage && localStorageAvailable()) {
192196
window.localStorage.setItem(lookupLocalStorage, lng);
193197
}
194198
}
@@ -198,7 +202,10 @@
198202
const sessionStorageAvailable = () => {
199203
if (hasSessionStorageSupport !== null) return hasSessionStorageSupport;
200204
try {
201-
hasSessionStorageSupport = window !== 'undefined' && window.sessionStorage !== null;
205+
hasSessionStorageSupport = typeof window !== 'undefined' && window.sessionStorage !== null;
206+
if (!hasSessionStorageSupport) {
207+
return false;
208+
}
202209
const testKey = 'i18next.translate.boo';
203210
window.sessionStorage.setItem(testKey, 'foo');
204211
window.sessionStorage.removeItem(testKey);
@@ -379,6 +386,7 @@
379386
if (this.services && this.services.languageUtils && this.services.languageUtils.getBestMatchFromCodes) return detected; // new i18next v19.5.0
380387
return detected.length > 0 ? detected[0] : null; // a little backward compatibility
381388
}
389+
382390
cacheUserLanguage(lng) {
383391
let caches = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.options.caches;
384392
if (!caches) return;

i18nextBrowserLanguageDetector.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/browserLookups/localStorage.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const localStorageAvailable = () => {
44
if (hasLocalStorageSupport !== null) return hasLocalStorageSupport;
55

66
try {
7-
hasLocalStorageSupport = window !== 'undefined' && window.localStorage !== null;
7+
hasLocalStorageSupport = typeof window !== 'undefined' && window.localStorage !== null;
8+
if (!hasLocalStorageSupport) { return false; }
89
const testKey = 'i18next.translate.boo';
910
window.localStorage.setItem(testKey, 'foo');
1011
window.localStorage.removeItem(testKey);
@@ -19,15 +20,15 @@ export default {
1920

2021
// Deconstruct the options object and extract the lookupLocalStorage property
2122
lookup({ lookupLocalStorage }) {
22-
if (localStorageAvailable() && lookupLocalStorage) {
23+
if (lookupLocalStorage && localStorageAvailable()) {
2324
return window.localStorage.getItem(lookupLocalStorage) || undefined; // Undefined ensures type consistency with the previous version of this function
2425
}
2526
return undefined;
2627
},
2728

2829
// Deconstruct the options object and extract the lookupLocalStorage property
2930
cacheUserLanguage(lng, { lookupLocalStorage }) {
30-
if (localStorageAvailable() && lookupLocalStorage) {
31+
if (lookupLocalStorage && localStorageAvailable()) {
3132
window.localStorage.setItem(lookupLocalStorage, lng);
3233
}
3334
}

src/browserLookups/sessionStorage.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const sessionStorageAvailable = () => {
44
if (hasSessionStorageSupport !== null) return hasSessionStorageSupport;
55

66
try {
7-
hasSessionStorageSupport = window !== 'undefined' && window.sessionStorage !== null;
7+
hasSessionStorageSupport = typeof window !== 'undefined' && window.sessionStorage !== null;
8+
if (!hasSessionStorageSupport) { return false; }
89
const testKey = 'i18next.translate.boo';
910
window.sessionStorage.setItem(testKey, 'foo');
1011
window.sessionStorage.removeItem(testKey);

0 commit comments

Comments
 (0)