mobxjs / mobx-angular

The MobX connector for Angular.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Local Storage access in IE11

mohsenvafa opened this issue · comments

It seems mobx-angular is using local storage for debugging purpose. In IE11 if accessing to local storage is restricted, Access Denied exception will be thrown. I found this issue by digging into the source code and I ended up in following code in mobx-angular-debug.js

// function for turning debug on / off
export var mobxAngularDebug = (function () {
    if (typeof localStorage === 'undefined' || typeof console === 'undefined' || typeof window === 'undefined') {
        return function () { };
    }
    if (!localStorage || !console || !window) {
        return function () { };
    }
    var style = 'background: #222; color: #bada55';
    window['mobxAngularDebug'] = function (value) {
        if (value) {
            console.log('%c MobX will now log everything to the console', style);
            console.log('%c Right-click any element to see its dependency tree', style);
            localStorage['mobx-angular-debug'] = true;
        }
        else
            delete localStorage['mobx-angular-debug'];
    };

I see there is a check to see if local storage is available but in IE11 it doesn't work all the time. If a user sets the security level to High, then local storage will not be accessible. In this case, you get AaccessDenied exception.

Is it possible to fix this issue? I think the following code should address this scenario:

 try
    {
        localStorage['mobx-angular-debug'];
    }
    catch (err) {        
        return function () { };
    }

Sounds like the correct fix.
@mohsenvafa, do you want to make a PR?

@adamkleingit This fix should be applied to a js file named mobx-angular-debug.js but I cannot find it in this repository. Looks like it is an external library, right?
The only place that is referencing to this file is in mobx-autorun.directive.ts which has been commented out:

image

Oh right, it was removed in MobX 4 :)
So you probably have an older version. Can you upgrade to the latest version?
If not - you can fork the older version that you have and introduce that fix