From aae89fcae79f5eea6841e9c34a20f043efe3f5b0 Mon Sep 17 00:00:00 2001 From: Tomasz Szustak Date: Wed, 1 Feb 2023 14:14:50 +0100 Subject: [PATCH] fix: Fixing localStorage overflow caused by unlimited logging of activityLog entries. Introduced sliding window of max 4000 items. --- bundles/CoreBundle/Resources/public/js/targeting.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/bundles/CoreBundle/Resources/public/js/targeting.js b/bundles/CoreBundle/Resources/public/js/targeting.js index c1edd4df584..e0255425c66 100644 --- a/bundles/CoreBundle/Resources/public/js/targeting.js +++ b/bundles/CoreBundle/Resources/public/js/targeting.js @@ -293,6 +293,19 @@ User.prototype.save = function () { if (util.featureDetect('localStorage')) { + // Ensure activityLog doesn't eat up all the local storage. + // It is assumed 10MB of localStorage group space, activityLog + // entries can vastly vary in size e.g. for pageView entries due + // to the saved URL. + // For now 0.5 KB per log entry are assumed. Using a + // conservative 20% of the assumed available space as limit + // allows us to fill 2MB of storage which equals about 4000 + // activityLog entries. + const activityLogLimit = 4000; + if (this.data.activityLog.length > activityLogLimit) { + util.logger.canLog('info') && console.info('[TARGETING] ' + this.data.activityLog.length + ' acitvityLog items exceed limit of ' + activityLogLimit + '. Evicting excess.', this); + this.data.activityLog = this.data.activityLog.slice(0, activityLogLimit); + } localStorage.setItem("_ptg.user", JSON.stringify(this.data)); }