Skip to content

Commit

Permalink
change localstorage check to try to address #297
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Feb 14, 2025
1 parent 9d765ed commit c24ea17
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 8.0.3

- change localstorage check to try to address [297](https://github.com/i18next/i18next-browser-languageDetector/issues/297)

### 8.0.2

- fix for when passed services are null, should address [296](https://github.com/i18next/i18next-browser-languageDetector/issues/296)
Expand Down
4 changes: 2 additions & 2 deletions i18nextBrowserLanguageDetector.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@
let {
lookupLocalStorage
} = _ref;
if (lookupLocalStorage && localStorageAvailable()) {
if (localStorageAvailable() && lookupLocalStorage) {
return window.localStorage.getItem(lookupLocalStorage) || undefined; // Undefined ensures type consistency with the previous version of this function
}
return undefined;
Expand All @@ -188,7 +188,7 @@
let {
lookupLocalStorage
} = _ref2;
if (lookupLocalStorage && localStorageAvailable()) {
if (localStorageAvailable() && lookupLocalStorage) {
window.localStorage.setItem(lookupLocalStorage, lng);
}
}
Expand Down
2 changes: 1 addition & 1 deletion i18nextBrowserLanguageDetector.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/browserLookups/localStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ export default {

// Deconstruct the options object and extract the lookupLocalStorage property
lookup({ lookupLocalStorage }) {
if (lookupLocalStorage && localStorageAvailable()) {
if (localStorageAvailable() && lookupLocalStorage) {
return window.localStorage.getItem(lookupLocalStorage) || undefined; // Undefined ensures type consistency with the previous version of this function
}
return undefined;
},

// Deconstruct the options object and extract the lookupLocalStorage property
cacheUserLanguage(lng, { lookupLocalStorage }) {
if (lookupLocalStorage && localStorageAvailable()) {
if (localStorageAvailable() && lookupLocalStorage) {
window.localStorage.setItem(lookupLocalStorage, lng);
}
}
Expand Down

3 comments on commit c24ea17

@danielrentz
Copy link
Contributor

@danielrentz danielrentz commented on c24ea17 Feb 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adrai I do not think that this will fix the problem. It just makes performance worse due to checking for localStorage support while the option is false(y).

But what about this line 7?

 hasLocalStorageSupport = window !== 'undefined' && window.localStorage !== null;
                        ^^^

I think there is a "typeof" missing. window will obviously always be different to a string :)

Same problem in sessionStorage btw

@adrai
Copy link
Member Author

@adrai adrai commented on c24ea17 Feb 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since I'm not able to reproduce your issue feel free to provide a PR

@danielrentz
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.