From 406175a410f5d80dc218e160fdd79cc53a926ff6 Mon Sep 17 00:00:00 2001 From: Paulo Reis Date: Wed, 27 Jul 2016 16:17:16 +0100 Subject: [PATCH] Protection against possible local storage write errors. --- services/web/public/coffee/main/event.coffee | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/services/web/public/coffee/main/event.coffee b/services/web/public/coffee/main/event.coffee index 46a443b4f5..f84ccdcc2a 100644 --- a/services/web/public/coffee/main/event.coffee +++ b/services/web/public/coffee/main/event.coffee @@ -4,14 +4,18 @@ define [ CACHE_KEY = "countlyEvents" _getEventCache = () -> - eventCache = window.localStorage.getItem CACHE_KEY + eventCacheStr = window.localStorage.getItem CACHE_KEY # Initialize as an empy object if the event cache is still empty. - if !eventCache? - window.localStorage.setItem(CACHE_KEY, "{}") - eventCache = window.localStorage.getItem CACHE_KEY + if !eventCacheStr? + eventCacheStr = "{}" - return JSON.parse eventCache + # Errors writing to localStorage may happen when quota is full or + # browser is in incognito mode. We'll return an empty object, anyway. + try + window.localStorage.setItem CACHE_KEY, eventCacheStr + + return JSON.parse eventCacheStr _eventInCache = (key) -> curCache = _getEventCache() @@ -25,7 +29,11 @@ define [ curCache = _getEventCache() curCache[key] = true curCacheAsStr = JSON.stringify curCache - window.localStorage.setItem CACHE_KEY, curCacheAsStr + + # Protection against issues mentioned above. + try + window.localStorage.setItem CACHE_KEY, curCacheAsStr + App.factory "event_tracking", ->