Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crashes applications when access to localStorage is blocked #41

Closed
DevinBarth-Zen opened this issue Mar 29, 2023 · 5 comments
Closed

Crashes applications when access to localStorage is blocked #41

DevinBarth-Zen opened this issue Mar 29, 2023 · 5 comments

Comments

@DevinBarth-Zen
Copy link

This causes applications to crash when access to localStorage has been blocked:

var storage = window.localStorage;

Causes: rudderlabs/rudder-sdk-js/issues/689

@DevinBarth-Zen
Copy link
Author

Suggestion from this comment would resolve: #8 (comment)

@jaywcjlove
Copy link
Owner

store.js/src/main.js

Lines 21 to 44 in 8ed05fb

function dealIncognito(storage = window.localStorage) {
var _KEY = '_Is_Incognit', _VALUE = 'yes';
try {
storage.setItem(_KEY, _VALUE);
storage.removeItem(_KEY);
} catch (e) {
Storage.prototype._data = {};
Storage.prototype.setItem = function (id, val) {
return this._data[id] = String(val);
};
Storage.prototype.getItem = function (id) {
return this._data.hasOwnProperty(id) ? this._data[id] : undefined;
};
Storage.prototype.removeItem = function (id) {
return delete this._data[id];
};
Storage.prototype.clear = function () {
return this._data = {};
}
storage = Storage;
}
finally { if (storage.getItem(_KEY) === _VALUE) storage.removeItem(_KEY); }
return storage;
}

@DevinBarth-Zen I've added not sure if it's solved, you can give it a try.

@bardisg
Copy link

bardisg commented Mar 31, 2023

Thank you for the release.

Will test latest version and update you soon!

@DevinBarth-Zen
Copy link
Author

This looks like it would still cause the same issue. I would suggest moving the default inside of your try/catch statement.

function dealIncognito(storage) { 
   var _KEY = '_Is_Incognit', _VALUE = 'yes'; 
   try { 
     // NOTE: set default storage when not passed in
     if (!storage) {
       storage = window.localStorage;
     }
     storage.setItem(_KEY, _VALUE); 
     storage.removeItem(_KEY); 
   } catch (e) { 
     Storage.prototype._data = {}; 
     Storage.prototype.setItem = function (id, val) { 
       return this._data[id] = String(val); 
     }; 
     Storage.prototype.getItem = function (id) { 
       return this._data.hasOwnProperty(id) ? this._data[id] : undefined; 
     }; 
     Storage.prototype.removeItem = function (id) { 
       return delete this._data[id]; 
     }; 
     Storage.prototype.clear = function () { 
       return this._data = {}; 
     } 
     storage = Storage; 
   } 
   finally { if (storage.getItem(_KEY) === _VALUE) storage.removeItem(_KEY); } 
   return storage; 
 } 

@DevinBarth-Zen
Copy link
Author

Closing this issue as it has been resolved!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants