From 46b25d5ab52b5b2fef1934de85e18af34df296ff Mon Sep 17 00:00:00 2001 From: Sam Dawson Date: Mon, 15 Aug 2022 11:33:29 +1000 Subject: [PATCH] Fix crash when third party cookies are disabled --- src/paho-mqtt.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/paho-mqtt.js b/src/paho-mqtt.js index 9ca3d37..a2c81a2 100644 --- a/src/paho-mqtt.js +++ b/src/paho-mqtt.js @@ -106,14 +106,19 @@ function onMessageArrived(message) { /** * @private */ - var localStorage = global.localStorage || (function () { - var data = {}; - - return { - setItem: function (key, item) { data[key] = item; }, - getItem: function (key) { return data[key]; }, - removeItem: function (key) { delete data[key]; }, - }; + var localStorage = (function () { + try { + // When third-party cookies are disabled accessing localStorage will cause an error + if (global.localStorage) return global.localStorage; + } catch (e) { + var data = {}; + + return { + setItem: function (key, item) { data[key] = item; }, + getItem: function (key) { return data[key]; }, + removeItem: function (key) { delete data[key]; }, + }; + } })(); /**