我正在使用 LESS CSS(更确切地说是 less.js),它似乎在后台利用了 LocalStorage。在本地运行我的应用程序时,我以前从未见过这样的错误,但现在我在每个页面显示时都会收到“达到持久存储最大大小”,就在我的应用程序的唯一 .less 文件的链接上方。
到目前为止,这只发生在 Firefox 12.0 上。
有没有办法解决这个问题?
PS:主要受计算 localStorage space 的使用启发,这就是我最终要做的(这是基于 Prototype 并依赖于自定义的琐碎 Logger 类,但这应该很容易适应您的上下文):
"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();
});
PPS:我还没有测量对 UI 的性能影响,但是可以创建一个装饰器并仅每 X 分钟执行一次存储测试(例如,在本地存储中执行的最后一个时间戳)。