mobxjs / mobx-angular

The MobX connector for Angular.
MIT License
483 stars 59 forks source link

Local Storage access in IE11 #75

Closed mohsenvafa closed 6 years ago

mohsenvafa commented 6 years ago

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 () { };
    }
adamkleingit commented 6 years ago

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

mohsenvafa commented 6 years ago

@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

adamkleingit commented 6 years ago

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