Skip to content

Commit 55cf306

Browse files
authored
Merge pull request #191 from trello/matt/wrap-localStorage
Handle cases where localStorage access is denied
2 parents f57c192 + 253be58 commit 55cf306

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/paho-mqtt.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function onMessageArrived(message) {
106106
/**
107107
* @private
108108
*/
109-
var localStorage = global.localStorage || (function () {
109+
var localStorage = (function () {
110110
var data = {};
111111

112112
return {
@@ -115,6 +115,14 @@ function onMessageArrived(message) {
115115
removeItem: function (key) { delete data[key]; },
116116
};
117117
})();
118+
try {
119+
if (global.localStorage) {
120+
localStorage = global.localStorage;
121+
}
122+
} catch (err) {
123+
// in browsers localStorage access can be disabled / blocked and even
124+
// reading the localStorage property will result in an Access denied error
125+
}
118126

119127
/**
120128
* Unique message type identifiers, with associated

0 commit comments

Comments
 (0)