celery/modules/03-cache.js
2024-07-28 06:37:14 -07:00

43 lines
1 KiB
JavaScript

const fs = require("fs");
const path = require("path");
const mtimes = new Map();
function version(file) {
const str = (time) => time.valueOf().toString(36);
try {
const where = path.resolve(__dirname + "/../overlay/" + file);
if(mtimes.has(file) && Date.now() - mtimes.get(file) < 5 * 1000) return str(mtimes.get(file));
const mtime = fs.statSync(where).mtime;
mtimes.set(file, mtime);
return str(mtime);
} catch {
const time = Date.now() - 1000 * 5;
mtimes.set(file, time);
return time.toString(36);
}
}
module.exports = (app, dir) => {
app.get("/assets/*", (req, res, next) => {
if(!req.query.v) {
res.redirect(req.path + "?v=" + version(req.path));
} else {
next();
}
});
// app.get("/cinny/*", (req, res, next) => {
// res.set("Cache-Control", "public, max-age=" + 60 * 60 * 24 * 365);
// next();
// });
app.get("*", (req, res, next) => {
if(req.query.v) {
res.set("Cache-Control", "public, max-age=" + 60 * 60 * 24 * 365);
}
next();
});
}
module.exports.version = version;