Merge pull request +11024 from c9/fix-cache

Fix cdn cache
pull/227/head
Lennart Kats 2015-12-09 14:29:58 +01:00
commit 0f501a8dc7
1 zmienionych plików z 23 dodań i 18 usunięć

Wyświetl plik

@ -70,9 +70,8 @@ function main(options, imports, register) {
var buffer = ""; var buffer = "";
var t = Date.now(); var t = Date.now();
var q = new FsQ(); var q = new FsQ();
var skinChanged = 0; var lastCssChange = 0;
var requestedSkin = ""; var compiledSkins = {};
var requestedSkinChanged = -1;
q.process = function(e) { q.process = function(e) {
var parts = e.split(" "); var parts = e.split(" ");
var id = parts[1]; var id = parts[1];
@ -81,8 +80,9 @@ function main(options, imports, register) {
if (path == id && !/^(\/|\w:)/.test(path)) { if (path == id && !/^(\/|\w:)/.test(path)) {
path = build.cacheDir + "/" + path; path = build.cacheDir + "/" + path;
if (/^\w+\/skin\//.test(id)) if (/^\w+\/skin\//.test(id)) {
requestedSkin = id; compiledSkins[id] = -1;
}
} }
fs.stat(path, function(err, s) { fs.stat(path, function(err, s) {
@ -94,11 +94,12 @@ function main(options, imports, register) {
} }
} }
if (err) { if (compiledSkins[id]) {
if (!skinChanged && /\.(css|less)/.test(id)) compiledSkins[id] = mt || -1;
skinChanged = s ? s.mtime.valueOf() : Infinity; }
if (requestedSkin == id) else if (err) {
requestedSkinChanged = s ? s.mtime.valueOf() : 0; if (/\.(css|less)/.test(id))
lastCssChange = Math.max(lastCssChange, s ? s.mtime.valueOf() : Infinity);
res.write(id + "\n"); res.write(id + "\n");
} }
q.oneDone(); q.oneDone();
@ -106,12 +107,14 @@ function main(options, imports, register) {
}; };
q.end = function() { q.end = function() {
if (!q.buffer.length && !q.active) { if (!q.buffer.length && !q.active) {
if (skinChanged >= requestedSkinChanged && requestedSkin) { if (compiledSkins) {
res.write(requestedSkin + "\n"); Object.keys(compiledSkins).forEach(function(key) {
console.info("Deleting old skin", requestedSkin); if (compiledSkins[key] < lastCssChange) {
return fs.unlink(build.cacheDir + "/" + requestedSkin, function() { res.write(key + "\n");
requestedSkin = ""; fs.unlink(build.cacheDir + "/" + key, function() {
q.end(); console.info("Deleting old skin", key);
});
}
}); });
} }
res.write("\n"); res.write("\n");
@ -218,7 +221,7 @@ function main(options, imports, register) {
type = "text/css"; type = "text/css";
res.setHeader("Content-Type", type); res.setHeader("Content-Type", type);
var mtime = Date.now(); var mtime = Math.floor(Date.now() / 1000) * 1000;
res.setHeader("ETAG", '"' + Buffer.byteLength(code) + "-" + mtime + '"'); res.setHeader("ETAG", '"' + Buffer.byteLength(code) + "-" + mtime + '"');
res.statusCode = 200; res.statusCode = 200;
res.end(code); res.end(code);
@ -235,7 +238,9 @@ function main(options, imports, register) {
console.log("File cached at", filename); console.log("File cached at", filename);
// set utime to have consistent etag // set utime to have consistent etag
fs.utimes(filename, mtime, mtime, function() {}); fs.utimes(filename, mtime/1000, mtime/1000, function(e) {
if (e) console.error(e);
});
}); });
}); });
}); });