Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Less CSS and local storage issue

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).

like image 385
fbiville Avatar asked May 08 '26 01:05

fbiville


1 Answers

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.

like image 122
Adam Avatar answered May 09 '26 15:05

Adam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!