Generate cache buster on build

v1.18.x
Peter Jaszkowiak 8 years ago
parent 001aa3d1a8
commit f4147f7922

@ -44,6 +44,8 @@ exports.build = function build(targets, callback) {
exports.buildTargets = function (targets, callback) {
var meta = require('./src/meta');
var cacheBuster = require('./src/meta/cacheBuster');
buildStart = buildStart || Date.now();
var step = function (startTime, target, next, err) {
@ -114,14 +116,21 @@ exports.buildTargets = function (targets, callback) {
return process.exit(1);
}
var time = (Date.now() - buildStart) / 1000;
cacheBuster.write(function (err) {
if (err) {
winston.error('[build] Failed to write `cache-buster.conf`: ' + err.message);
return process.exit(1);
}
winston.info('[build] Asset compilation successful. Completed in ' + time + 's.');
var time = (Date.now() - buildStart) / 1000;
if (typeof callback === 'function') {
callback();
} else {
process.exit(0);
}
winston.info('[build] Asset compilation successful. Completed in ' + time + 's.');
if (typeof callback === 'function') {
callback();
} else {
process.exit(0);
}
});
});
};

@ -0,0 +1,46 @@
'use strict';
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var winston = require('winston');
var filePath = path.join(__dirname, '../../build/cache-buster');
var cached;
// cache buster is an 11-character, lowercase, alphanumeric string
function generate() {
return (Math.random() * 1e18).toString(32).slice(0, 11);
}
exports.write = function write(callback) {
mkdirp(path.dirname(filePath), function (err) {
if (err) {
return callback(err);
}
fs.writeFile(filePath, generate(), callback);
});
};
exports.read = function read(callback) {
if (cached) {
return callback(null, cached);
}
fs.readFile(filePath, function (err, buffer) {
if (err) {
winston.warn('[cache-buster] could not read cache buster: ' + err.message);
return callback();
}
buffer = buffer.toString();
if (buffer) {
cached = buffer;
return callback(null, cached);
}
callback();
});
};

@ -6,7 +6,7 @@ var nconf = require('nconf');
var db = require('../database');
var pubsub = require('../pubsub');
var utils = require('../../public/src/utils');
var cacheBuster = require('./cacheBuster');
module.exports = function (Meta) {
@ -21,10 +21,16 @@ module.exports = function (Meta) {
Meta.configs.list(next);
},
function (config, next) {
config['cache-buster'] = 'v=' + utils.generateUUID();
cacheBuster.read(function (err, buster) {
if (err) {
return next(err);
}
config['cache-buster'] = 'v=' + (buster || Date.now());
Meta.config = config;
setImmediate(next);
Meta.config = config;
next();
});
}
], callback);
};

Loading…
Cancel
Save