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

IE LocalStorage.setItem bug with compression #271

Open
lianee opened this issue Mar 2, 2018 · 3 comments
Open

IE LocalStorage.setItem bug with compression #271

lianee opened this issue Mar 2, 2018 · 3 comments

Comments

@lianee
Copy link

lianee commented Mar 2, 2018

Using store.everything.min.js 2.0.12, Edge (IE11 also) throw an exception "SCRIPT87: Invalid argument" with the following code:

var x = [
  {"id":"10","keys":"dev"}
];
store.set('test', x);

crash happens line 22 of localStorage.js

It seems that there's a char in the compressed string that IE doesn't like too much. No problem with Chrome or Firefox.

jsfiddle

@RonenNess
Copy link

I'm having the same issue on Edge. Since I had to fix it ASAP (it was blocking my checkout page for some users) I added the following patch right after including store.js:

try
{
    store.set("__test_store_", [{"id":"10","keys":"dev"}])
}
catch (e)
{
    console.warn("Store fallback!");
    store = {
        get: function(key)
        {
            return JSON.parse(localStorage.getItem(key));
        },
        set: function(key, val)
        {
            localStorage.setItem(key, JSON.stringify(val));
        },
    }
}

Its not a good solution but its better than nothing for now..
Hope this problem will have a solution soon.

@ensemblebd
Copy link

ensemblebd commented Jun 12, 2018

Excellent, thank you for the patch!

I made some adjustments for my use case scenario to hook the func and attempt to use it preferentially

window.storageReadyCallback = function(storage) {
	// internet explorer fallback, due to poor browser code by microsoft.
	try {
		storage.set("__test_store_", [{"id":"10","keys":"dev"}])
	}
	catch (e) {
		console.warn("Store fallback!");
		storage._get = storage.get;
		storage.get = function(key) {
			var r=null;
			try {
				r = storage._get(key);
			} catch (E) {
				r = JSON.parse(localStorage.getItem(key));
			}
			return r;
		};
		storage._set = storage.set;
		storage.set = function(key, val) {
			try {
				storage._set(key, val);
			} catch (E) {
				localStorage.setItem(key, JSON.stringify(val));
			}
		}
	}
};

@climff
Copy link

climff commented May 28, 2019

This is probably a little old but just found out from LZString you can do the following (code from http://pieroxy.net/blog/pages/lz-string/guide.html):

var string = "This is my compression test.";
alert("Size of sample is: " + string.length);
var compressed = LZString.compressToUTF16(string);
alert("Size of compressed sample is: " + compressed.length);
localStorage.setItem("myData",compressed);
(...)
string = LZString.decompressFromUTF16(localStorage.getItem("myData"));
alert("Sample is: " + string);

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

4 participants