gsklee / ngStorage

localStorage and sessionStorage done right for AngularJS.
MIT License
2.33k stars 461 forks source link

Error on clear through native interface from inside iframe #164

Closed sbnedkov closed 9 years ago

sbnedkov commented 9 years ago

Hi,

I've been experiencing this strange error that seems related to some race condition on one side but I was able to reproduce it pretty frequently. The error manifests itself with a:

TypeError: event.key is null

at

https://github.com/gsklee/ngStorage/blob/master/ngStorage.js#L193

Here is the cleanest setup I was able to create for reproducing, happens in both Firefox and Chrome:

index.html:

<html>
    <head>
        <script type="application/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.5/angular.js"></script>
        <script type="application/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/ngStorage/0.3.9/ngStorage.js"></script>
        <script type="application/javascript">
            angular.module('test', ['ngStorage'])
                .controller('Test', ['$localStorage', function ($localStorage) {
                    $localStorage.test = '123';
                }]);
        </script>
    </head>
    <body ng-app="test">
        <iframe ng-src="./iframe.html" ng-controller="Test"></iframe>
    </body>
</html>

iframe.html:

<html>
    <body>
        <script type="application/javascript">
            localStorage.clear();
        </script>
    </body>
</html>

I'm curious to hear what might be the reason, is this a browser bug?

Cheers.

egilkh commented 9 years ago

Hey! Thanks for the bug report.

I guess we'll need to safeguard event.key with a check for defined/null to be sure that this error does not happen.

I don't think it's a browser bug, I'm thinking it might be just a event without a key that is to tell the listeners about the clear and not a change to any key.

sbnedkov commented 9 years ago

Yes, this will fix it, I just tried. Tell me if I can help test a solution.