From f4147f7922e1f19b1f442546f2225b7bc3193b48 Mon Sep 17 00:00:00 2001
From: Peter Jaszkowiak
Date: Mon, 23 Jan 2017 21:06:34 -0700
Subject: [PATCH] Generate cache buster on build
---
build.js | 23 ++++++++++++++-------
src/meta/cacheBuster.js | 46 +++++++++++++++++++++++++++++++++++++++++
src/meta/configs.js | 14 +++++++++----
3 files changed, 72 insertions(+), 11 deletions(-)
create mode 100644 src/meta/cacheBuster.js
diff --git a/build.js b/build.js
index 73013231d5..89c55177ad 100644
--- a/build.js
+++ b/build.js
@@ -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);
+ }
+ });
});
};
\ No newline at end of file
diff --git a/src/meta/cacheBuster.js b/src/meta/cacheBuster.js
new file mode 100644
index 0000000000..ca00fba057
--- /dev/null
+++ b/src/meta/cacheBuster.js
@@ -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();
+ });
+};
diff --git a/src/meta/configs.js b/src/meta/configs.js
index 75da0595d2..907e0ef477 100644
--- a/src/meta/configs.js
+++ b/src/meta/configs.js
@@ -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);
};