3

I'm using LESS CSS (more exactly less.js) which seems to exploit LocalStorage under the hood. I had never seen such an error like this before while running my app locally, but now I get "Persistent storage maximum size reached" at every page display, just above the link the unique .less file of my app.

This only happens with Firefox 12.0 so far.

Is there any way to solve this?

P.S.: mainly inspired by Calculating usage of localStorage space, this is what I ended up doing (this is based on Prototype and depends on a custom trivial Logger class, but this should be easily adapted in your context):

"use strict";
var LocalStorageChecker = Class.create({
    testDummyKey: "__DUMMY_DATA_KEY__",
    maxIterations: 100,
    logger: new Logger("LocalStorageChecker"),
    analyzeStorage: function() {
        var result = false;
        if (Modernizr.localstorage && this._isLimitReached()) {  
            this._clear();
        }
        return result;
    },
    _isLimitReached: function() {
        var localStorage = window.localStorage;
        var count = 0;
        var limitIsReached = false;
        do {
            try {
                var previousEntry = localStorage.getItem(this.testDummyKey);
                var entry = (previousEntry == null ? "" : previousEntry) + "m";
                localStorage.setItem(this.testDummyKey, entry);
            }
            catch(e) {
                this.logger.debug("Limit exceeded after " + count + " iteration(s)");
                limitIsReached = true;
            }
        }
        while(!limitIsReached && count++ < this.maxIterations);
        localStorage.removeItem(this.testDummyKey);
        return limitIsReached;
    },
    _clear: function() {
        try {
            var localStorage = window.localStorage;
            localStorage.clear();
            this.logger.debug("Storage clear successfully performed");
        }
        catch(e) {
            this.logger.error("An error occurred during storage clear: ");
            this.logger.error(e);
        }
    }
});


document.observe("dom:loaded",function() {
    var checker = new LocalStorageChecker();
    checker.analyzeStorage();
});

P.P.S.: I didn't measure the performance impact on the UI yet, but a decorator could be created and perform the storage test only every X minutes (with the last timestamp of execution in the local storage for instance).

Community
  • 1
  • 1
fbiville
  • 6,836
  • 5
  • 48
  • 72

2 Answers2

1

Here is a good resource for the error you are running into.

http://www.sitepoint.com/building-web-pages-with-local-storage/#fbid=5fFWRXrnKjZ

Gives some insight that localstorage only has so much room and you can max it out in each browser. Look into removing some data from localstorage to resolve your problem.

Adam
  • 3,301
  • 6
  • 28
  • 50
1

Less.js persistently caches content that is @imported. You can use this script to clear content that is cached. Using the script below you can call the function destroyLessCache('/path/to/css/') and it will clear your localStorage of css files that have been cached.

    function destroyLessCache(pathToCss) { // e.g. '/css/' or '/stylesheets/'
       if (!window.localStorage || !less || less.env !== 'development') {
            return;
       }
       var host = window.location.host;
       var protocol = window.location.protocol;
       var keyPrefix = protocol + '//' + host + pathToCss;
       for (var key in window.localStorage) {
          if (key.indexOf(keyPrefix) === 0) {
             delete window.localStorage[key];
          }
       }
    }
retornam
  • 329
  • 1
  • 8